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
Copy after login
Methods
Now, let us see some of the commonly used methods in the Java Collections Class.
-
addAll(Collection super T> c, T… elements): Every element will be added to the mentioned collection on calling this method.
-
binarySearch(List extends Comparable super T>> list, T key): Mentioned list will be searched for the specified object with the help of a binary search algorithm.
-
binarySearch(List extends T> list, T key, Comparator super T> 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 super T> dest, List extends T> 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 super T> 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 extends T> c): An unmodifiable view will be returned for the collection mentioned.
-
unmodifiableList (List extends T> list): An unmodifiable view will be returned for the list mentioned.
-
unmodifiableMap (Map extends K,? extends V> m): An unmodifiable view will be returned for the map mentioned.
-
unmodifiableSet (Set extends T> 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 extends T> coll): The largest element of the collection will be returned based on the element’s natural ordering.
-
max(Collection extends T> coll, Comparator super T> comp): The largest element of the collection will be returned based on the comparator provided.
-
min(Collection extends T> coll): The minimum element of the collection will be returned based on the element’s natural ordering.
-
min(Collection extends T> coll, Comparator super T> 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 super T> 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!