更新時間:2021-10-26 10:12:30 來源:動力節點 瀏覽782次
Java 提供了一個工具類,用于操作 Set、List、Map 等集合:Collections。該工具類提供了大量對集合元素進行排序、查詢和修改的方法。它還提供了對集合對象進行同步控制的方法。常用方法:
example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsTest {
public static void main(String[] args) {
//Create a List collection of Integer type
List<Integer> list = new ArrayList<>();
//Add data to it
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(3);
list.add(4);
list.add(5);
//Print collection
System.out.println("--------------Collection------------------");
System.out.println(list);//[1, 2, 3, 4, 5, 3, 4, 5]
System.out.println("--------------reverse------------------");
Collections.reverse(list);//Reverse sort
System.out.println(list);//[5, 4, 3, 5, 4, 3, 2, 1]
System.out.println("--------------shuffle------------------");
Collections.shuffle(list);//Shuffle the order randomly
System.out.println(list);//[3, 4, 1, 5, 5, 4, 3, 2]
System.out.println("--------------sort------------------");
Collections.sort(list);//Naturally sort, sort the list in ascending order
System.out.println(list);//[1, 2, 3, 3, 4, 4, 5, 5]
System.out.println("--------------sort(List list,Comparator c)------------------");
//Sort the specified list according to the order generated by the specified comparator
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//TODO Auto-generated method stub
return o2-o1;//Descending order
}
});
System.out.println(list);//[5, 5, 4, 4, 3, 3, 2, 1]
System.out.println("--------------swap------------------");
Collections.swap(list, 0, 7);//Swap 0 and 7 indexed elements
System.out.println(list);//[1, 5, 4, 4, 3, 3, 2, 5]
System.out.println("--------------rotate------------------");
Collections.rotate(list, 2);//Move back 2 positions
System.out.println(list);//[2, 5, 1, 5, 4, 4, 3, 3]
System.out.println("--------------binarySearch------------------");
//Find the index for the value in the parentheses
System.out.println(Collections.binarySearch(list, 5));//3
System.out.println("--------------max min------------------");
System.out.println(Collections.max(list));//5 maximum
System.out.println(Collections.min(list));//1 minimum
//fill
System.out.println("--------------fill------------------");
//Collections.fill(list,0);//Replace all elements
System.out.println(list);//[0, 0, 0, 0, 0, 0, 0, 0]
System.out.println("--------------frequency------------------");
//Return the number of given elements
System.out.println(Collections.frequency(list,5));//2
System.out.println("--------------indexOfSubList, lastIndexOfSubList------------------");
List<Integer> list2 = new ArrayList<Integer>();
list2.add(1);
list2.add(5);
//If there is a piece of list2 data in the list collection, there is an index return
System.out.println(Collections.indexOfSubList(list, list2));//2 first
System.out.println(Collections.lastIndexOfSubList(list, list2));//2 last
//replaceAll replace all 5 to 0
System.out.println("--------------replaceAll------------------");
Collections.replaceAll(list, 5, 0);
System.out.println(list);
}
}
結果如下:
如果大家想了解更多相關知識,不妨來關注一下動力節點的Java開發工具頁面,里面有更多的工具可以介紹,大家可以了解一下。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習