Java basic class: Collections tool class

catalogue

1,Collections

1.1. Sorting: (all static methods)

1.2. Search and replace

1.3 synchronous control

1.4. Return immutable set

1,Collections

Collections is a tool class that operates on collections such as Set, List, and Map.

Collections provides a series of static methods to sort, query and modify collection elements. It also provides methods to set immutable collection objects and realize synchronous control over collection objects.

1.1. Sorting: (all static methods)

  1. reverse(List): reverses the order of elements in the List
  2. shuffle(List): randomly sort the elements of the List collection
  3. sort(List): sorts the elements of the specified List set in ascending order according to the natural order of the elements
  4. sort(List, Comparator): sort the List collection elements according to the order generated by the specified Comparator
  5. swap(List, int, int): exchange the elements at i and j in the specified list set
public static void main(String[] args) {
        List<String> list = Arrays.asList("a b c d e f".split(" "));
        Collections.reverse(list);
        System.out.println(list);
        }
}
//Operation result: [f, e, d, c, b, a]

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add("c");
    list.add("d");
    list.add("b");
    list.add("a");
    Collections.shuffle(list);  
    System.out.println(list);
 }

//[d, c, a, b]

swap(List list,int i,int j)Method (exchange elements in the collection whose specified element index is unknown)
public class TestCollection {
    public static void main(String[] args) {
    List<String> list = Arrays.asList("a b c d e f".split(" "));
    Collections.swap(list, 2, 3);
    System.out.println(list);
}
//Operation result: [a, b, d, c, e, f]

1.2. Search and replace

  1. Object max(Collection): returns the largest element in a given collection according to the natural order of elements
  2. Object max(Collection, Comparator): returns the data in the order specified by the Comparator
    The maximum number of elements in a given collection
  3. Object min(Collection)
  4. Object min(Collection,Comparator)
  5. int frequency(Collection, Object): returns the number of occurrences of the specified element in the specified collection
  6. void copy(List dest,List src): copy the contents of src to dest
  7. Boolean replaceall (List, Object oldVal, Object newVal): replaces all old values of the List object with new values

1.3 synchronous control

         The Collections class provides multiple static methods that can wrap them into a thread synchronized collection. The synchronizedXxx() method can wrap the specified collection into a thread synchronized collection, which can solve the thread safety problem when multiple threads access the collection concurrently

//Returns the synchronous (thread safe) collection supported by the specified collection
synchronizedCollection(Collection<T>  c) 
synchronizedList(List<T> list)//Returns a List of synchronization (thread safe) supported by the specified List
synchronizedMap(Map<K,V> m) //Returns a synchronized (thread safe) Map supported by the specified Map
synchronizedSet(Set<T> s) //Returns the synchronous (thread safe) set supported by the specified set

shortcoming

  • Each read and write must be locked. The lock level is an object, which is not conducive to multi-threaded simultaneous operation
  • Modify the element ConcurrentModificationException when traversing with Iterator
  • It is recommended to use the collection thread synchronization class of java.util.concurrent

  It is better not to use the following methods. The efficiency is very low. When thread safe collection types are required, please consider using the concurrent collection under the JUC package.

1.4. Return immutable set

Returns an empty, immutable collection object, where the collection can be List,Set,Map
emptyXxx()

Returns an immutable collection object that contains only the specified object (only one or one element), which can be List,Set,Map
singletonXxx()

Returns the immutable view of the specified collection object. The collection here can be: List,Set,Map. 
unmodifiableXxx()
The parameters of the above three methods are the original collection object, and the return value is the "read-only" version of the collection.

Tags: Collections

Posted on Mon, 20 Sep 2021 02:57:45 -0400 by naveendk.55