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开发工具页面,里面有更多的工具可以介绍,大家可以了解一下。
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习