Yige

Yige

Build

Basic Concepts of Java Collection Classes

Basic Concepts of Java Collection Classes#

The major categories of the collection framework in Java can be 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 must 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: Access is ordered, indexed, and values can be retrieved based on the index; elements can be duplicated
  • Set: Access is unordered, and elements cannot be duplicated

List#

Includes:

  • ArrayList: Implemented based on an array, fast query speed, slow add/delete speed
  • LinkedList: Implemented based on a linked list structure, slow query speed, fast add/delete speed, provides special methods for operations on the head and tail elements (for adding, deleting, and querying), not thread-safe
  • Vector: Array structure, fast query, slow add/delete, thread-safe, thus lower efficiency (deprecated)

Set#

Characteristics of Set collection: elements are unique, access is unordered, no index. The Set collection includes:

  • HashSet: Unordered storage, no index for elements, elements cannot be duplicated. Implemented based on 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, elements are unique, access is unordered (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)#

  • 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)#

  • binarySearch(List list, Object key): Uses binary search to obtain the index of the specified object in the List, provided the collection is 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 there are no common elements in the two specified collections.
  • 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 forces the specified comparator's order to be reversed. If the specified comparator is null, this method is equivalent to reverseOrder() (in other words, it returns a comparator that forces the natural order of objects implementing the Comparable interface in the collection to be reversed).
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.