1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | package com.hanchao.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.List; import java.util.Vector; /** * java.util.Collections的简单学习 * @author liweihan (liweihan@sohu-inc.com) * @version 1.0 (2016年1月13日 上午10:20:51) */ public class TestCollections { public static void main(String[] args) { /** * java.util.Collections 是一个包装类。它包含有各种有关集合的操作的静态多态方法。 * 此类不能实例化,就像一个工具类,服务于java的Collection框架。 * * 参考: * http://www.apihome.cn/api/java/Collections.html * */ /** * 1.排序sort() * 根据元素的自然顺序 对指定列表按升序进行排序。列表中的所有元素都必须实现 Comparable 接口。 * 此外,列表中的所有元素都必须是可相互比较的(也就是说, * 对于列表中的任何 e1 和 e2 元素,e1.compareTo(e2) * 不得抛出 ClassCastException */ List<Double> list = new ArrayList<Double>(); list.add(112D); list.add(113D); list.add(23D); list.add(456D); list.add(112D); list.add(231D); Collections.sort(list); for ( int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } //结果:23.0 112.0 113.0 231.0 456.0 /** * 2.混排 * 混排算法所做的正好与 sort 相反: 它打乱在一个 List 中可能有的任何排列的踪迹。 * 也就是说,基于随机源的输入重排该 List,这样的排列具有相同的可能性(假设随机源是公正的)。 * 这个算法在实现一个碰运气的游戏中是非常有用的。 * 例如,它可被用来混排代表一副牌的Card 对象的一个 List 。另外,在生成测试案例时,它也是十分有用的。 */ Collections.shuffle(list); System.out.println( "混排:" ); for ( int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } //结果:113.0 456.0 112.0 23.0 112.0 231.0 /** * 3.反转reverse() * 使用Reverse方法可以根据元素的自然顺序 对指定列表按降序进行排序。 */ Collections.reverse(list); System.out.println( "反转:" ); for ( int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } //结果:231.0 112.0 23.0 112.0 456.0 113.0 /** * 4.fill() * 使用指定元素替换指定列表中的所有元素。 */ List<String> list2 = new ArrayList<String>(); list2.add( "aa" ); list2.add( "bb" ); list2.add( "cc" ); list2.add( "dd" ); Collections.fill(list2, "hanchao" ); System.out.println( "替换:" ); for (String string : list2) { System.out.println(string); } //结果:hanchao hanchao hanchao hanchao /** * 5.copy() * 将所有元素从一个列表复制到另一个列表。 * 执行此操作后,目标列表中每个已复制元素的索引将等同于源列表中该元素的索引。 * 目标列表的长度至少必须等于源列表。 * 如果目标列表更长一些,也不会影响目标列表中的其余元素。 * */ List<String> list3 = new ArrayList<String>(); list3.add( "list31" ); list3.add( "list32" ); list3.add( "list33" ); List<String> list4 = new ArrayList<String>(); list4.add( "copy1" ); list4.add( "copy2" ); list4.add( "copy3" ); // list4.add("copy4"); Collections.copy(list3, list4); //list4是源 ,list4的长度必须<=list3.size() System.out.println( "复制:" ); for (String str : list3) { System.out.println(str); } /** * 6.min() * 根据元素的自然顺序 返回给定 collection 的最小元素。 * collection 中的所有元素都必须实现 Comparable 接口。 * 此外,collection 中的所有元素都必须是可相互比较的(也就是说, * 对于 collection 中的任意 e1 和 e2 元素,e1.compareTo(e2) * 不得抛出 ClassCastException)。 * * max() * 根据元素的自然顺序,返回给定 collection 的最大元素。 * collection 中的所有元素都必须实现 Comparable 接口。 * 此外,collection 中的所有元素都必须是可相互比较的(也就是说, * 对于 collection 中的任意 e1 和 e2 元素, * e1.compareTo(e2) 不得抛出 ClassCastException)。 */ List<Double> list5 = new ArrayList<Double>(); list5.add(112D); list5.add(113D); list5.add(23D); list5.add(456D); list5.add(112D); list5.add(231D); System.out.println( "最小元素:" + Collections.min(list5)); System.out.println( "最大元素:" + Collections.max(list5)); /** * 7.indexOfSubList(List,list) * 返回指定源列表中第一次出现指定目标列表的起始位置; * 如果没有出现这样的列表,则返回 -1。 * * lastIndexOfSubList(List,list) * 返回指定源列表中最后一次出现指定目标列表的起始位置; * 如果没有出现这样的列表,则返回 -1。 * * binarySearch(list,key) * 查找指定集合中的元素,返回所查找元素的索引。 * */ double array[] = { 112 , 111 , 23 , 456 , 111 , 231 }; List<Double> list6 = new ArrayList<Double>(); List<Double> li = new ArrayList<Double>(); for ( int i = 0 ; i < array.length; i++) { list6.add( new Double(array[i])); } double arr[] = { 111 }; for ( int j= 0 ;j<arr.length;j++){ li.add( new Double(arr[j])); } int locations = Collections.lastIndexOfSubList (list6,li); int locations2 = Collections.indexOfSubList(list6, li); int locations3 = Collections.binarySearch(list6, 23d); System.out.println( "=== lastIndexOfSubList:" + locations); System.out.println( "=== indexOfSubList:" + locations2); System.out.println( "=== binarySearch:" + locations3); //结果:=== lastIndexOfSubList:4 //结果:=== indexOfSubList:1 //结果:=== binarySearch:2 /** * 8.rotate() * 根据指定的距离轮换指定列表中的元素。调用此方法后, * 对于 0 和 list.size()-1(包括)之间的所有 i 值, * 索引 i 处的元素将是以前位于索引 (i - distance) mod list.size() 处的元素。 * (此方法对列表的大小没有任何影响。) * */ String[] str = { "t" , "a" , "n" , "k" , "s" }; List<String> list7 = new ArrayList<String>(); for ( int i = 0 ; i < str.length; i++) { list7.add(str[i]); } // Collections.rotate(list7, -4);//如果是负数,则左移 Collections.rotate(list7, 2 ); //如果是正数,则右移 System.out.println( "位移:" ); for (String s : list7) { System.out.println(s); } /** * 9.replaceAll(list,old,new) * 替换指定的元素,若要替换的值存在则返回true,反之则返回false * */ List<String> list8 = Arrays.asList( "one two three four five six siven" .split( " " )); System.out.println(list8); //[one, two, three, four, five, six, siven] Collections.replaceAll(list8, "siven" , "siven eight nine" ); System.out.println( "---:" + list8); //---:[one, two, three, four, five, six, siven eight nine] /** * 10.swap(List list,int i,int j) * 在指定列表的指定位置处交换元素。 * (如果指定位置相同,则调用此方法不会更改列表。) * */ List<String> list10 = Arrays.asList( "one two three four five six siven" .split( " " )); System.out.println( "***:" + list10); //***:[one, two, three, four, five, six, siven] Collections.swap(list10, 3 , 5 ); System.out.println( "---:" + list10); //---:[one, two, three, six, five, four, siven] /** * 11 .nCopies( int n,Object 0 ) * 返回大小为n的List ,List不可改变,其中的所有引用都指向o */ List<String> list11 = new ArrayList<String>(); list11 = Collections.nCopies( 5 , "hello" ); System.out.println( "$$$:" + list11); //$$$:[hello, hello, hello, hello, hello] /** * 12.enumeration(Collection) * 返回一个指定 collection 上的枚举。 * 此方法提供与遗留 API 的互操作性,遗留 API 需要一个枚举作为输入。 */ List<String> list12 = Arrays.asList( "I love you xiao jie !" .split( " " )); System.out.println( "---:" + list12); //---:[I, love, you, xiao, jie, !] Enumeration<String> e = Collections.enumeration(list12); Vector<String> v = new Vector<String>(); while (e.hasMoreElements()) { v.addElement(e.nextElement()); } System.out.println( " v :" + v); //v :[I, love, you, xiao, jie, !] } } |