![]() |
VOOZH | about |
The Collections class in Java is a utility class provided by the Java Collections Framework that contains static methods for performing common operations on collections. It simplifies tasks like sorting, searching, and modifying collection elements efficiently.
[Apple, Banana, Apple]
Explanation: An ArrayList of type String is created and elements are added to it, including a duplicate value. The output shows that ArrayList maintains insertion order and allows duplicate elements.
Object is the parent class of all the classes.
Collection Framework contains both classes and interfaces. Although both seem the same but there are certain differences between Collection classes and the Collections framework.
The Collection classes in Java are mentioned below:
Lists are ordered collections that allow duplicates.
Sets are unordered collections that do not allow duplicates.
Queues hold elements prior to processing, typically following FIFO, but can support priority.
Maps store key-value pairs with unique keys. Not part of the Collection interface but part of JCF.
These classes provide common functionalities and simplify implementation of concrete collection classes.
After Adding: [Apple, Banana, Mango, Grapes]
Explanation
After Removing: [Mango, Grapes]
Explanation
Mango is present in the list
Explanation
After Updating: [Orange, Banana, Mango]
Explanation
After Sorting: [Apple, Banana, Grapes, Mango]
Explanation:
Methods | Description |
|---|---|
| addAll(Collection<? super T> c, T... elements) | This method is used to insert the specified collection elements to the specified collection. |
| asLifoQueue(Deque<T> deque) | This method returns a view of a Deque as a Last-in-first-out (Lifo) Queue. |
| binarySearch(List<? extends Comparable> list, T key) | This method searches the key using binary search in the specified list. |
| binarySearch(List<? extends T> list, T key, Comparator<? super T> c) | This method searches the specified list for the specified object using the binary search algorithm. |
| checkedCollection(Collection<E> c, Class<E> type) | This method returns a dynamically typesafe view of the specified collection. |
| checkedList(List<E> list, Class<E> type) | This method returns a dynamically typesafe view of the specified list. |
| checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType) | This method returns a dynamically typesafe view of the specified map. |
| checkedNavigableMap(NavigableMap<K,V> m, Class<K> keyType, Class<V> valueType) | This method returns a dynamically typesafe view of the specified navigable map. |
| checkedNavigableSet(NavigableSet<E> s, Class<E> type) | This method returns a dynamically typesafe view of the specified navigable set. |
| checkedQueue(Queue<E> queue, Class<E> type) | This method returns a dynamically typesafe view of the specified queue. |
| checkedSet(Set<E> s, Class<E> type) | This method returns a dynamically typesafe view of the specified set. |
| checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType) | This method returns a dynamically typesafe view of the specified sorted map. |
| checkedSortedSet(SortedSet<E> s, Class<E> type) | This method returns a dynamically typesafe view of the specified sorted set. |
| copy(List<? super T> dest, List<? extends T> src) | This method copies all of the elements from one list into another. |
| disjoint(Collection<?> c1, Collection<?> c2) | This method returns true if the two specified collections have no elements in common. |
| emptyEnumeration() | This method returns an enumeration that has no elements. |
| emptyIterator() | This method returns an iterator that has no elements. |
| emptyList() | This method returns an empty list (immutable). |
| emptyListIterator() | This method returns a list iterator that has no elements. |
| emptyMap() | This method returns an empty map (immutable). |
| emptyNavigableMap() | This method returns an empty navigable map (immutable). |
| emptyNavigableSet() | This method returns an empty navigable set (immutable). |
| emptySet() | This method returns an empty set (immutable). |
| emptySortedMap() | This method returns an empty sorted map (immutable). |
| emptySortedSet() | This method returns an empty sorted set (immutable). |
| enumeration(Collection<T> c) | This method returns an enumeration over the specified collection. |
| fill(List<? super T> list, T obj) | This method replaces all of the elements of the specified list with the specified element. |
| frequency(Collection<?> c, Object o) | This method returns the number of elements in the specified collection equal to the specified object. |
| indexOfSubList(List<?> source, List<?> target) | This method returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. |
| lastIndexOfSubList(List<?> source, List<?> target) | This method returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. |
| list(Enumeration<T> e) | This method returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. |
| max(Collection<? extends T> coll) | This method returns the maximum element of the given collection, according to the natural ordering of its elements. |
| max(Collection<? extends T> coll, Comparator<? super T> comp) | This method returns the maximum element of the given collection, according to the order induced by the specified comparator. |
| min(Collection<? extends T> coll) | This method returns the minimum element of the given collection, according to the natural ordering of its elements. |
| min(Collection<? extends T> coll, Comparator<? super T> comp) | This method returns the minimum element of the given collection, according to the order induced by the specified comparator. |
| nCopies(int n, T o) | This method returns an immutable list consisting of n copies of the specified object. |
| newSetFromMap(Map<E,Boolean> map) | This method returns a set backed by the specified map. |
| replaceAll(List<T> list, T oldVal, T newVal) | This method replaces all occurrences of one specified value in a list with another. |
| reverse(List<?> list) | This method reverses the order of the elements in the specified list |
| reverseOrder() | This method returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface. |
| reverseOrder(Comparator<T> cmp) | This method returns a comparator that imposes the reverse ordering of the specified comparator. |
| rotate(List<?> list, int distance) | This method rotates the elements in the specified list by the specified distance. |
| shuffle(List<?> list) | This method randomly permutes the specified list using a default source of randomness. |
| shuffle(List<?> list, Random rnd) | This method randomly permute the specified list using the specified source of randomness. |
| singletonMap(K key, V value) | This method returns an immutable map, mapping only the specified key to the specified value. |
| singleton(T o) | This method returns an immutable set containing only the specified object. |
| singletonList(T o) | This method returns an immutable list containing only the specified object. |
| sort(List<T> list) | This method sorts the specified list into ascending order, according to the natural ordering of its elements. |
| sort(List<T> list, Comparator<? super T> c) | This method sorts the specified list according to the order induced by the specified comparator. |
| swap(List<?> list, int i, int j) | This method swaps the elements at the specified positions in the specified list. |
| synchronizedCollection(Collection<T> c) | This method returns a synchronized (thread-safe) collection backed by the specified collection. |
| synchronizedList(List<T> list) | This method returns a synchronized (thread-safe) list backed by the specified list. |
| synchronizedMap(Map<K,V> m) | This method returns a synchronized (thread-safe) map backed by the specified map. |
| synchronizedNavigableMap(NavigableMap<K,V> m) | This method returns a synchronized (thread-safe) navigable map backed by the specified navigable map. |
| synchronizedNavigableSet(NavigableSet<T> s) | This method returns a synchronized (thread-safe) navigable set backed by the specified navigable set. |
| synchronizedSet(Set<T> s) | This method returns a synchronized (thread-safe) set backed by the specified set. |
| synchronizedSortedMap(SortedMap<K,V> m) | This method returns a synchronized (thread-safe) sorted map backed by the specified sorted map. |
| synchronizedSortedSet(SortedSet<T> s) | This method returns a synchronized (thread-safe) sorted set backed by the specified sorted set. |
| unmodifiableCollection(Collection<? extends T> c) | This method returns an unmodifiable view of the specified collection. |
| unmodifiableList(List<? extends T> list) | This method returns an unmodifiable view of the specified list. |
| unmodifiableNavigableMap(NavigableMap<K,? extends V> m) | This method returns an unmodifiable view of the specified navigable map. |
| unmodifiableNavigableSet(NavigableSet<T> s) | This method returns an unmodifiable view of the specified navigable set. |
| unmodifiableSet(Set<? extends T> s) | This method returns an unmodifiable view of the specified set. |
| unmodifiableSortedMap(SortedMap<K,? extends V> m) | This method returns an unmodifiable view of the specified sorted map. |
| unmodifiableSortedSet(SortedSet<T> s) | This method returns an unmodifiable view of the specified sorted set. |