Collections: Tool classes for manipulating Collection and Map
The difference between Collections and Collection
Collections is a tool class used to manipulate Collection and Map.
- Common methods in the Collections class.
reverse(List): Reverse the order of the elements in
List shuffle(List): Randomly arrange the order in the set (every time is random)
sort(list): Sort according to natural order, if you want to change it The method needs to use sort (list, Comparator)
swap (i, j); exchange the position of element i and element j in the List collection.
frequency(i); Returns the number of occurrences of the specified element in the collection.
copy(list1,list2); copy list2 to list1
public static void main(String[] args) {
List list = new ArrayList();
list.add("1");
list.add("1");
list.add("1");
list.add("1");
list.add("1");
list.add("1");
// ArrayList<Object> objects = new ArrayList<Object>();
//直接复制会导致出错,是因为objects集合的大小小于list集合。
List dest = Arrays.asList(new Object[list.size()]);
System.out.println(dest);
Collections.copy(dest,list);
System.out.println(dest);
//返回线程安全的list
List list1 = Collections.synchronizedList(list);
}