Collections則是集合類的一個工具類/幫助類,其中提供了一系列靜態方法,用於對集合中元素進行排序、搜索以及線程安全等各類操做。安全
一、 sort(Collection)方法的使用(含義:對集合進行排序)。 例:對已知集合c進行排序? public class Practice { public static void main(String[] args){ List c = new ArrayList(); c.add("l"); c.add("o"); c.add("v"); c.add("e"); System.out.println(c); Collections.sort(c); System.out.println(c); } } 運行結果爲:[l, o, v, e] [e, l, o, v]
二、 shuffle(Collection)方法的使用(含義:對集合進行隨機排序)。 例:shuffle(Collection)的簡單示例? public class Practice { public static void main(String[] args){ List c = new ArrayList(); c.add("l"); c.add("o"); c.add("v"); c.add("e"); System.out.println(c); Collections.shuffle(c); System.out.println(c); Collections.shuffle(c); System.out.println(c); } } 運行結果爲:[l, o, v, e] [l, v, e, o] [o, v, e, l]
三、 binarySearch(Collection,Object)方法的使用(含義:查找指定集合中的元素,返回所查找元素的索引)。 例:binarySearch(Collection,Object)的簡單示例? public class Practice { public static void main(String[] args){ List c = new ArrayList(); c.add("l"); c.add("o"); c.add("v"); c.add("e"); System.out.println(c); int m = Collections.binarySearch(c, "o"); System.out.println(m); } } 運行結果爲:[l, o, v, e] 1
四、 replaceAll(List list,Object old,Object new) 方法的使用(含義:替換批定元素爲某元素,若要替換的值存在剛返回true,反之返回false)。 例: public class Practice { public static void main(String[] args){ List list = Arrays.asList("one two three four five six siven".split(" ")); System.out.println(list); List subList = Arrays.asList("three four five six".split(" ")); System.out.println(Collections.replaceAll(list, "siven", "siven eight")); System.out.println(list); } } 運行結果爲: [one, two, three, four, five, six, siven] true [one, two, three, four, five, six, siven eight]
五、 reverse()方法的使用(含義:反轉集合中元素的順序)。 例: public class Practice { public static void main(String[] args){ List list = Arrays.asList("one two three four five six siven".split(" ")); System.out.println(list); Collections.reverse(list); System.out.println(list); } } 運行結果爲: [one, two, three, four, five, six, siven] [siven, six, five, four, three, two, one]
六、 rotate(List list,int m)方法的使用(含義:集合中的元素向後移m個位置,在後面被遮蓋的元素循環到前面來)。 例: public class Practice { public static void main(String[] args){ List list = Arrays.asList("one two three four five six siven".split(" ")); System.out.println(list); Collections.rotate(list, 1); System.out.println(list); } } 運行結果爲: [one, two, three, four, five, six, siven] [siven, one, two, three, four, five, six]
七、 copy(List m,List n)方法的使用(含義:將集合n中的元素所有複製到m中,而且覆蓋相應索引的元素)。 例: public class Practice { public static void main(String[] args){ List m = Arrays.asList("one two three four five six siven".split(" ")); System.out.println(m); List n = Arrays.asList("我 是 複製過來的哈".split(" ")); System.out.println(n); Collections.copy(m,n); System.out.println(m); } } 運行結果爲:[one, two, three, four, five, six, siven] [我, 是, 複製過來的哈] [我, 是, 複製過來的哈, four, five, six, siven]
八、 swap(List list,int i,int j)方法的使用(含義:交換集合中指定元素索引的位置)。 例: public class Practice { public static void main(String[] args){ List m = Arrays.asList("one two three four five six siven".split(" ")); System.out.println(m); Collections.swap(m, 2, 3); System.out.println(m); } } 運行結果爲: [one, two, three, four, five, six, siven] [one, two, four, three, five, six, siven]
九、 fill(List list,Object o)方法的使用(含義:用對象o替換集合list中的全部元素)。 例: public class Practice { public static void main(String[] args){ List m = Arrays.asList("one two three four five six siven".split(" ")); System.out.println(m); Collections.fill(m, "啊啊啊"); System.out.println(m); } } 運行結果爲: [one, two, three, four, five, six, siven] [啊啊啊, 啊啊啊, 啊啊啊,啊啊啊, 啊啊啊, 啊啊啊, 啊啊啊]
十、 nCopies(int n,Object o)方法的使用(含義:返回大小爲n的List,List不可改變,其中的全部引用都指向o)。 例: public class Practice { public static void main(String[] args){ System.out.println(Collections.nCopies(5, "嘿嘿")); } } 運行結果爲: [嘿嘿, 嘿嘿, 嘿嘿, 嘿嘿, 嘿嘿]
十一、addAll(Collection,T)方法的使用(含義:將全部指定元素添加到指定 collection 中。) 例: public class Practice{ public static void main (String[] args){ ArrayList<String> a = new ArrayList(); Collections.addAll(a, "小白","小王","小黑","張三"); System.out.println(a); } } 運行結果爲: [小白, 小王, 小黑, 張三]