1、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]
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);
}
}
運行結果為:
[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)方法的使用(含義:對集合進行隨機排序)。
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]
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);
}
}
運行結果為:
[one, two, three, four, five, six, siven]
[青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍]
5.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]
6.min(Collection),min(Collection,Comparator)方法的使用(前者采用Collection內(nèi)含自然比較法,后者采用Comparator進行比較)。
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));
}
運行結果:[l, o, v, e]
e
7.max(Collection),max(Collection,Comparator)方法的使用(前者采用Collection內(nèi)含自然比較法,后者采用Comparator進行比較)。
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));
}
運行結果:[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));
}
運行結果: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));
}
運行結果:7
10.rotate(List list,int m)方法的使用(含義:集合中的元素向后移m個位置,在后面被遮蓋的元素循環(huán)到前面來)。移動列表中的元素,負數(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);
}
運行結果:[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);
}
運行結果:[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);
}
}
運行結果為:
[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);
}
}
運行結果為:[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);
}
}
運行結果為:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
總結
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站名稱:關于java中Collections類方法-創(chuàng)新互聯(lián)
URL地址:http://muchs.cn/article42/cdogec.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、小程序開發(fā)、網(wǎng)站營銷、網(wǎng)站設計公司、品牌網(wǎng)站建設、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)