Basic Concepts of Java Collection Classes#
The collection framework in Java can be broadly divided into Collection
and Map
; the differences between the two are:
- Collection is a single-column collection; Map is a double-column collection
- Only the Set series in Collection requires unique elements; in Map, keys need to be unique, while values can be duplicated
- The data structure of Collection is element-oriented; the data structure of Map is key-oriented.
Collection#
Collection includes two major systems:
List
: Ordered access, indexed, can retrieve values based on index, elements can be duplicatedSet
: Unordered access, elements cannot be duplicated
List#
Includes:
ArrayList
: Implemented with an underlying array, fast query speed, slow addition and deletionLinkedList
: Implemented based on a linked list structure, slow query speed, fast addition and deletion, provides special methods for operations on the head and tail elements (for addition, deletion, and querying), not thread-safeVector
: Array structure, fast query, slow addition and deletion, thread-safe, thus low efficiency (deprecated)
Set#
Characteristics of Set collection: elements are not duplicated, unordered access, no index. The Set collection includes:
HashSet
: Unordered storage, elements have no index, elements cannot be duplicated. The underlying structure is a hash table.LinkedHashSet
: Implemented based on both a linked list and a hash table, thus having ordered access and unique elements.TreeSet
: Based on a binary tree data structure, unique elements, unordered access (can be sorted when adding elements).
Map#
Map is a double-column collection that stores key-value pairs, where keys must remain unique, and values can be duplicated, including:
HashMap
LinkedHashMap
TreeMap
Utility Class Collections#
The Collections utility class provides a large number of operations for Collection/Map.
Sorting Operations (mainly for List interface related)#
reverse(List list)
: Reverses the order of elements in the specified List collection.shuffle(List list)
: Randomly sorts the elements in the List (shuffling).sort(List list)
: Sorts the elements in the List in natural ascending order.sort(List list, Comparator c)
: Sorts using a custom comparator.swap(List list, int i, int j)
: Swaps the elements at positions i and j in the specified List collection.rotate(List list, int distance)
: Moves all elements to the right by the specified length; if distance equals size, the result remains unchanged.
Searching and Replacing (mainly for Collection interface related)#
binarySearch(List list, Object key)
: Uses binary search to obtain the index of the specified object in the List, provided the collection is already sorted.max(Collection col)
: Returns the maximum element.max(Collection col, Comparator comp)
: Returns the maximum element based on a custom comparator.min(Collection col)
: Returns the minimum element.min(Collection col, Comparator comp)
: Returns the minimum element based on a custom comparator.fill(List list, Object obj)
: Fills with the specified object.frequency(Collection Object o)
: Returns the number of occurrences of the specified object in the specified collection.replaceAll(List list, Object old, Object new)
: Replaces.
Setting Immutable Collections#
Collections has three types of methods that can return an immutable collection:
emptyXxx()
: Returns an empty immutable collection object.singletonXxx()
: Returns an immutable collection object that contains only the specified object.unmodifiableXxx()
: Returns an immutable view of the specified collection object.
Others#
disjoint(Collection<?> c1, Collection<?> c2)
: Returns true if the two specified collections have no elements in common.addAll(Collection<? super T> c, T... a)
: A convenient way to add all specified elements to the specified collection.Comparator<T> reverseOrder(Comparator<T> cmp)
: Returns a comparator that reverses the order of the specified comparator. If the specified comparator is null, this method is equivalent to reverseOrder() (in other words, it returns a comparator that forces a reversal of the natural order on objects that implement the Comparable interface).