Home  >  Article  >  Java  >  Java Collections Class

Java Collections Class

WBOY
WBOYOriginal
2024-08-30 15:46:151114browse

In Java, an individual object’s group is represented as one unit known as Collection. This framework defines multiple classes and interfaces to denote a group of objects as a single unit. In addition to that, several other features are also present for the Java Collections Class. It includes:

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Two important root interfaces are the Collection interface and Map interface of the packages java.util.Collection and java.util.Map respectively.
  • If the collections of objects of the class are null, then NullPointerException will be thrown.
  • Polymorphic algorithms are supported.

Declaration and Methods of Java Collections Class

More details on the Java Collections Class will be discussed in the following sections.

Declaration

Java Collections Class can be declared using the below syntax:

Syntax:

public class Collections extends Object

Methods

Now, let us see some of the commonly used methods in the Java Collections Class.

  • addAll(Collection c, T… elements): Every element will be added to the mentioned collection on calling this method.
  • binarySearch(List> list, T key): Mentioned list will be searched for the specified object with the help of a binary search algorithm.
  • binarySearch(List list, T key, Comparator c): Mentioned list will be searched for the specified object with the help of a binary search algorithm.
  • asLifoQueue(Deque deque): Deque view will be returned similar to LIFO(Last-in-First-Out) queue.
  • checkedCollection(Collection c, Class type): The typesafe view, which is dynamic, will be returned for the collection mentioned.
  • checkedList(List list, Class type): The typesafe view, which is dynamic, will be returned for the list mentioned.
  • checkedMap(Map m, Class keyType, Class valueType): The typesafe view which is dynamic will be returned for the map mentioned.
  • checkedSet(Set s, Class type): The typesafe view, which is dynamic, will be returned for the set mentioned.
  • checkedSortedMap(SortedMap m, Class keyType, Class valueType): The typesafe view which is dynamic will be returned for the sorted map mentioned.
  • checkedSortedSet(SortedSet s, Class type): The typesafe view, which is dynamic, will be returned for the sorted set mentioned.
  • emptyEnumeration(): An enumeration will be returned which has no elements.
  • emptyIterator(): An iterator will be returned which has no elements.
  • emptyList(): An empty list will be returned, which is immutable.
  • emptyListIterator(): A list iterator will be returned which has no elements.
  • emptyMap(): An empty map will be returned, which is immutable.
  • emptySet(): An empty set will be returned, which is immutable.
  • copy(List dest, List src): Elements from one list will be copied into another.
  • disjoint(Collection c1, Collection c2): If no elements are common for the mentioned two collections, true will be returned.
  • enumeration(Collection c): An enumeration will be returned over the collection mentioned.
  • fill(List list, T obj): Elements of the list mentioned will be replaced with the element mentioned.
  • frequency(Collection c, Object o): Count of the elements in the collection will be returned that are equal to the object mentioned.
  • lastIndexOfSubList(List source, List target): The beginning position of the last occurrence of the target list mentioned will be returned. If no such occurrences are there, -1 will be returned.
  • indexOfSubList(List source, List target): The beginning position of the first occurrence of the target list mentioned will be returned. If no such occurrences are there, -1 will be returned.
  • list(Enumeration e): An array list will be returned, which contains the elements returned by the enumeration mentioned in the order where the enumeration is returned.
  • unmodifiableCollection (Collection c): An unmodifiable view will be returned for the collection mentioned.
  • unmodifiableList (List list): An unmodifiable view will be returned for the list mentioned.
  • unmodifiableMap (Map m): An unmodifiable view will be returned for the map mentioned.
  • unmodifiableSet (Set s): An unmodifiable view will be returned for the set mentioned.
  • unmodifiableSortedMap (SortedMap m): An unmodifiable view will be returned for the sorted map mentioned.
  • unmodifiableSortedSet (SortedSet s): An unmodifiable view will be returned for the sorted set mentioned.
  • max(Collection coll): The largest element of the collection will be returned based on the element’s natural ordering.
  • max(Collection coll, Comparator comp): The largest element of the collection will be returned based on the comparator provided.
  • min(Collection coll): The minimum element of the collection will be returned based on the element’s natural ordering.
  • min(Collection coll, Comparator comp): The minimum element of the collection will be returned based on the comparator provided.
  • replaceAll(List list, T oldVal, T newVal): Every occurrence of the value mentioned in the list will be replaced with another.
  • nCopies(int n, T o): An immutable list will be returned, containing n copies of the object mentioned.
  • newSetFromMap(Map< E,Boolean> map): A set will be returned, which is backed by the map mentioned.
  • reverse(List list): Elements in the list mentioned will be reversed.
  • reverseOrder(): A comparator will be returned that makes the objects ordering reverse that can implement a Comparable interface.
  • shuffle(List list): The list mentioned will be shuffled based on the randomness.
  • reverseOrder (Comparator cmp): A comparator will be returned that makes the specified comparator’s ordering reverse.
  • rotate(List list, int distance): Elements in the list mentioned will be rotated by the distance specified.
  • shuffle(List list, Random rnd): The list mentioned will be permuted randomly based on the randomness.
  • singleton(T o): An immutable set will be returned that contains only the object mentioned.
  • singletonList(T o): An immutable list will be returned that contains only the object mentioned.
  • singletonMap(K key, V value): An immutable map will be returned that maps only the key mentioned to a particular value.
  • sort(List list): The list mentioned will be sorted based on the natural ordering.
  • sort(List list, Comparator c): The list mentioned will be sorted based on the comparator mentioned.
  • swap(List list, int i, int j): Elements in the list mentioned will be swapped based on the positions mentioned.
  • synchronizedCollection (Collection c): A synchronized collection that is thread-safe will be returned, which is backed by the collection mentioned.
  • synchronizedList (List list): A synchronized list that is thread-safe will be returned, which is backed by the list mentioned.
  • synchronizedMap (Map m): A synchronized map that is thread-safe will be returned, which is backed by the map mentioned.
  • synchronizedSet (Set s): A synchronized set that is thread-safe will be returned, which is backed by the set mentioned.
  • synchronizedSortedMap (SortedMap m): A synchronized sorted map that is thread-safe will be returned, which is backed by the sorted map mentioned.
  • synchronizedSortedSet (SortedSet s): A synchronized sorted set that is thread-safe will be returned, which is backed by the sorted set mentioned.

The above is the detailed content of Java Collections Class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is TreeMap in Java?Next article:What is TreeMap in Java?