Collections is a utility class created to make it easier to use Collections.

1, sort sort

1.1 the principle

Sort the given set C using the underlying sort method of the Arrays. This part of the Java Collections framework Arrays approach has been covered in previous articles

1.2 example

public static void main(String[] args){ List c = new ArrayList(); C.a. dd (" learning "); c.add("Java"); C.a. dd (" "); C. dd(" little sister "); System.out.println(c); Collections.sort(c); System.out.println(c); }Copy the code

2. The reversal reverse ()

2.1 Source Code Analysis

Reverses the order of the elements in a collection.

public static void reverse(List<? > list) { int size = list.size(); if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) { for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--) swap(list, i, j); } else { // instead of using a raw type here, it's possible to capture // the wildcard but it will require a call to a supplementary // private method ListIterator fwd = list.listIterator(); ListIterator rev = list.listIterator(size); for (int i=0, mid=list.size()>>1; i<mid; i++) { Object tmp = fwd.next(); fwd.set(rev.previous()); rev.set(tmp); }}}Copy the code

REVERSE_THRESHOLD (18); REVERSE_THRESHOLD (18); REVERSE_THRESHOLD (18);

@SuppressWarnings({"rawtypes", "unchecked"}) public static void swap(List<? > list, int i, int j) { // instead of using a raw type here, it's possible to capture // the wildcard but it will require a call to a supplementary // private method final List l = list; l.set(i, l.set(j, l.get(i))); }Copy the code

2. Use two iterators to iterate from the beginning and end respectively for element exchange; The iterator here supports moving the pointer forward.

2.2 example

public static void main(String[] args){ List c = new ArrayList(); C.a. dd (" learning "); c.add("Java"); C.a. dd (" "); C. dd(" little sister "); System.out.println(c); Collections.reverse(c); System.out.println(c); }Copy the code

3. Maximum value Max and minimum value min

3.1 Source Code Analysis

Let’s take the Max method as an example. Max provides two types of methods, one that requires an external collator and one that does not require a collator but requires elements in a collection to enforce the Comparable interface. The generic definition of the latter is interesting. We can learn two things: Type T is defined very smartly to mean that a generic type must inherit Object and implement Comparable. We can then determine whether or not a Comparable interface has been implemented in the method. This will only be known at runtime. The generic directly defines that we must implement the Comparable interface. We can tell the user at compile time that the current class does not implement the Comparable interface, which is user-friendly to use. 2. We are provided with a good example of implementing two sorting mechanisms: the first uses natural comparison within a Collection, and the second uses Comparator

3.2 example

public static void main(String[] args){ List c = new ArrayList(); C.a. dd (" learning "); c.add("Java"); C.a. dd (" "); C. dd(" little sister "); System.out.println(c); System.out.println(Collections.max(c)); }Copy the code

4. Thread-safe collections

Collections provides many thread-safe Collections for lists, maps, and sets. Let’s look at thread-safe Collections first

SynchronizedList synchronizedList synchronizedList synchronizedList synchronizedList synchronizedList synchronizedList synchronizedList synchronizedList synchronizedList

5. Immutable sets

Methods to get immutable collections all start with unmodifiable. What this kind of method means is that from the original set, we get a new set that is immutable, that we can only access, we can’t modify; Once modified, an exception is thrown. This is mainly because only the query method is open and any other modification will throw an exception,

Let’s take an example of unmodifiableList, where the underlying layer throws an exception directly.

​​​​