您好,登錄后才能下訂單哦!
1、sort(Collection)方法的使用(含義:對集合進(jìn)行排序)。
例:對已知集合c進(jìn)行排序
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);
}
}
運(yùn)行結(jié)果為:[l, o, v, e]
[e, l, o, v]
2.reverse()方法的使用(含義:反轉(zhuǎn)集合中元素的順序)。
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 list = Arrays.asList("one two three four five six siven".split(" "));//兩個空格
System.out.println(list);
List list = Arrays.asList("one two three four five six siven".split(" "));//一個空格
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
}
運(yùn)行結(jié)果為:
[o, n, e, , t, w, o, , t, h, r, e, e, , f, o, u, r, , f, i, v, e, , s, i, x, , s, i, v, e, n]
[one two three four five six siven]
[one, two, three, four, five, six, siven]
[siven, six, five, four, three, two, one]
3.shuffle(Collection)方法的使用(含義:對集合進(jìn)行隨機(jī)排序)。
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);
}
}
運(yùn)行結(jié)果為:[l, o, v, e]
[l, v, e, o]
[o, v, e, l]
4.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, "青鳥52T25小龍");
System.out.println(m);
}
}
運(yùn)行結(jié)果為:
[one, two, three, four, five, six, siven]
[青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍]
5.copy(List m,List n)方法的使用(含義:將集合n中的元素全部復(fù)制到m中,并且覆蓋相應(yīng)索引的元素)。
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("我 是 復(fù)制過來的哈".split(" "));
System.out.println(n);
Collections.copy(m,n);
System.out.println(m);
}
}
運(yùn)行結(jié)果為:[one, two, three, four, five, six, siven]
[我, 是, 復(fù)制過來的哈]
[我, 是, 復(fù)制過來的哈, four, five, six, siven]
6.min(Collection),min(Collection,Comparator)方法的使用(前者采用Collection內(nèi)含自然比較法,后者采用Comparator進(jìn)行比較)。
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);
System.out.println(Collections.min(c));
}
運(yùn)行結(jié)果:[l, o, v, e]
e
7.max(Collection),max(Collection,Comparator)方法的使用(前者采用Collection內(nèi)含自然比較法,后者采用Comparator進(jìn)行比較)。
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);
System.out.println(Collections.max(c));
}
運(yùn)行結(jié)果:[l, o, v, e]
v
8.indexOfSubList(List list,List subList)方法的使用(含義:查找subList在list中首次出現(xiàn)位置的索引)。
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.indexOfSubList(intList, targetList));
}
運(yùn)行結(jié)果:5
9.lastIndexOfSubList(List source,List target)方法的使用與上例方法的使用相同
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.lastIndexOfSubList(intList, targetList));
}
運(yùn)行結(jié)果:7
10.rotate(List list,int m)方法的使用(含義:集合中的元素向后移m個位置,在后面被遮蓋的元素循環(huán)到前面來)。移動列表中的元素,負(fù)數(shù)向左移動,正數(shù)向右移動
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(intList);
Collections.rotate(intList, 1);
System.out.println(intList);
}
運(yùn)行結(jié)果:[1, 2, 3, 4, 5]
[5, 1, 2, 3, 4]
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(intList);
Collections.rotate(intList, -1);
System.out.println(intList);
}
運(yùn)行結(jié)果:[1, 2, 3, 4, 5]
[2, 3, 4, 5, 1]
11.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);
}
}
運(yùn)行結(jié)果為:
[one, two, three, four, five, six, siven]
[one, two, four, three, five, six, siven]
12.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);
}
}
運(yùn)行結(jié)果為:[l, o, v, e]
1
13.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);
}
}
運(yùn)行結(jié)果為:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
總結(jié)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。