Overview: 1. collections (note that it is not a Collection, but a Collection, but an additional s) 2. it is a collection tool Class 3. method classification: regular operations (search, maximum, minimum, etc.), sorting, thread security (synchronization) operations, immutable set Java code: package com. cxy. collection; import java. util. arrayList; import java. util. arrays; import java. util. collections; import java. util. hashMap; import java. util. hashSet; import java. util. list; import java. util. map; import java. util. set; import java. util. treeSet;/*** @ author cxy */public c Lass CollectionsTest {public static void main (String [] args) {List l = new ArrayList (); l. add (1, 100); l. add (-66); l. add (0); l. add (88); System. out. println ("list:" + l); Collections. reverse (l); System. out. println ("reverse list:" + l); Collections. shuffle (l); // random sorting, shuffling System. out. println ("unordered list:" + l); Collections. swap (l, 1, 3); System. out. println ("the interchange number is the list after 1, 3 elements:" + l); Collections. sort (l); // here is a natural sorting, more sorting content See JAVA application Sorting System in this blog. out. println ("sorted list:" + l); Collections. binarySearch (l, 88); // binary search. The list must be in an ordered state. If the query is successful, the sequence number is returned. If no result is found, the System returns a negative number. out. println ("the smallest element in the list is:" + Collections. min (l); System. out. println ("the largest element in the list is:" + Collections. max (l); // the above two methods are natural sorting. Of course, you can implement a Comparator implementation class as the second parameter. For details, see JAVA application sorting. add (88); // 88 (the second parameter) the number of times System has occurred in list (the first parameter. out. println ("88 appears in the list:" + Collections. frequency (L, 88) + "times"); Collections. replaceAll (l, 88, 66); // replace 88 with 66. out. println ("Alternative list:" + l); Collections. fill (l, 66); // replace all elements in the list with 66 (the second parameter. System. out. println ("all the elements in the list are replaced with 66:" + l); // the following code indicates creating a l1 with the size of l. If it is not written in this way, an exception is reported, because List l1 = new ArrayList (Arrays. asList (new Object [l. size ()]); Collections. copy (l1, l); System. out. println ("Copy l l1:" + l1); System. out. println ("are l and l1 equal? "+ L. equals (l1); System. out. println (" is l and l1 a reference? "+ (L = l1); l1 = l; System. out. println (" are l and l1 equal? "+ L. equals (l1); System. out. println (" is l and l1 a reference? "+ (L = l1); l1 = new ArrayList (l); // This is a shortest copy. The references of l and l1 are different, however, the element references in l and l1 are still the same System. out. println ("are l and l1 equal? "+ L. equals (l1); System. out. println (" is l and l1 a reference? "+ (L = l1); // after some of the preceding problems are complex, a topic is displayed. // create a set of security types, the following indicates that this set can only add Integer data. add ("abc"); // It's okay now ~ Try {Collections. checkedList (l, Integer. class ). add ("abc");} catch (Exception e) {System. out. println ("an exception occurred when you try to add non-Integer data after type security operations");} System. out. println ("============================= "); /* pay attention to common collections (HashSet, HashMap, ArrayList, TreeMap, TreeSet, and history list). * these collections are NOT thread-safe, if your program is in a multi-threaded environment and may modify the same set at the same time, * You need to use Collections. synchronizedXxx method to ensure thread security */List sl = Collections. synchronizedList (new ArrayLis T (); Map sm = Collections. synchronizedMap (new HashMap (); Set ss = Collections. synchronizedSet (new HashSet (); Set sts = Collections. synchronizedSortedSet (new TreeSet ();/* immutable set (empty set, specified Element Set, immutable state) * 1. use Collections. emptyXxx method to create an unchangeable empty Set * 2. null immutable set: it does not bring unexpected exceptions because of null values. In my personal understanding, it is the best practice of initialization. * 3. specify an element set: returns a set that only contains the specified element. It is also an unchangeable Set * Application: creates an unchangeable special object set, for example, an administrator set, in this way, the Administrator object can have some collection methods. * For example, I can determine whether the user object is administrator (adminList. contains (user) * 4. immutable status: Get the immutable attempt of this set (read-only) * 3. here we use the list as an example. Other Collections are similar to */List <String> el = Collections. emptyList (); System. out. println (el. isEmpty (); try {el. add ("1"); // If you try to change it, The UnsupportedOperationException will be thrown.} catch (Exception e) {System. out. println ("UnsupportedOperationException");} List <String> el1 = null; // we may usually get used to defining a list try {el1.contains ("abc "); // We may accidentally use it somewhere (assuming it exists in a low probability)} catch (Exception e) www.2cto.com {System. out. println ("null exception");} List <String> singletonL = Collections. singletonList ("abcd"); System. out. println (singletonL); // you can get an unchangeable (read-only) set through the following method (the view feeling is actually described officially) list <String> listView = Collections. unmodifiableList (l );}}