In Java, a collection is a group of objects that can be manipulated as a single entity. In generally, Collection interface is considered as root interface of collection framework. It defines most common methods which are applicable for any collection object. There are total 9 direct or indirect child interface along with Collection interface.
1. Collection (I):The Collection interface is the root interface in the Java Collections Framework, and it defines the basic operations that can be performed on a collection such as adding, removing, and accessing elements. There are several direct child interfaces of the Collection interface that provide more specialized functionality.
2. List (I):A List is an ordered collection that allows duplicates. It allows access to elements by their index position, and provides methods to add, remove, and modify elements at specific positions. Examples of List implementations include ArrayList and LinkedList.
3. Set (I):A Set is a collection that does not allow duplicates. It provides methods to add and remove elements, and to test whether an element is a member of the set. Examples of Set implementations include HashSet and TreeSet.
4. Queue (I):A Queue is a collection that orders its elements in a specific way. It provides methods to add elements to the end of the queue, remove elements from the front of the queue, and inspect the element at the front of the queue. Examples of Queue implementations include LinkedList and PriorityQueue.
5. Map (I):A Map is a collection that maps keys to values. It provides methods to add, remove, and retrieve values associated with a given key. Examples of Map implementations include HashMap and TreeMap.
The main differences between these interfaces lie in their functionality and the way they handle their elements. For example, List allows duplicates and maintains the order of elements, while Set does not allow duplicates and does not guarantee any specific order. Map associates values with keys, while Queue orders its elements based on a specific rule.
In addition to the four indirect child interfaces of the Collection interface, there are two additional interfaces that extend Set and Map:
6. SortedSet (I):A SortedSet is a Set that maintains its elements in a sorted order. It provides additional methods for accessing elements based on their position in the sorted set. Examples of SortedSet implementations include TreeSet.
7. NavigableSet (I):A NavigableSet is a SortedSet that provides additional navigation methods for accessing elements. Examples of NavigableSet implementations include TreeSet.
Similarly, there are two additional interfaces that extend Map:
8. SortedMap (I):A SortedMap is a Map that maintains its keys in a sorted order. It provides additional methods for accessing entries based on their position in the sorted map. Examples of SortedMap implementations include TreeMap.
9. NavigableMap (I):A NavigableMap is a SortedMap that provides additional navigation methods for accessing entries. Examples of NavigableMap implementations include TreeMap.
Here's an example that demonstrates the use of these interfaces:
import java.util.*;
public class CollectionExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("cherry");
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("cherry");
SortedSet<String> sortedSet = new TreeSet<>();
sortedSet.add("apple");
sortedSet.add("banana");
sortedSet.add("cherry");
NavigableSet<String> navigableSet = new TreeSet<>();
navigableSet.add("apple");
navigableSet.add("banana");
navigableSet.add("cherry");
Queue<String> queue = new LinkedList<>();
queue.add("apple");
queue.add("banana");
queue.add("cherry");
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
SortedMap<String, Integer> sortedMap = new TreeMap<>();
sortedMap.put("apple", 1);
sortedMap.put("banana", 2);
sortedMap.put("cherry", 3);
NavigableMap<String, Integer> navigableMap = new TreeMap<>();
navigableMap.put("apple", 1);
navigableMap.put("banana", 2);
navigableMap.put("cherry", 3);
System.out.println("List: " + list);
System.out.println("Set: " + set);
System.out.println("SortedSet: " + sortedSet);
System.out.println("NavigableSet: " + navigableSet);
System.out.println("Queue: " + queue);
System.out.println("Map: " + map);
System.out.println("SortedMap: " + sortedMap);
System.out.println("NavigableMap: " + navigableMap);
}
}
Output:
List: [apple, banana, cherry]
Set: [cherry, apple, banana]
SortedSet: [apple, banana, cherry]
NavigableSet: [apple, banana, cherry]
Queue: [apple, banana, cherry]
Map: {cherry=3, apple=1, banana=2}
SortedMap: {apple=1, banana=2, cherry=3}
NavigableMap: {apple=1, banana=2, cherry=3}
In this example, we create a List, Set, Queue, and Map and add some elements to each of them. We then print the contents of each collection. Note that the order of elements in the Set is different from the order in which they were added, and the Map does not maintain any specific order.
here are some common methods present in the Collection interfaces in Java:
1. add(E element): Adds the specified element to the collection, if it is not already present.
2. remove(Object obj): Removes the specified element from the collection, if it is present.
3. size(): Returns the number of elements in the collection.
4. contains(Object obj): Returns true if the collection contains the specified element.
5. isEmpty(): Returns true if the collection is empty.
6. clear(): Removes all of the elements from the collection.
7. iterator(): Returns an iterator over the elements in the collection.
8. toArray(): Returns an array containing all of the elements in the collection.
9. addAll(Collection<? extends E> c): Adds all of the elements in the specified collection to this collection.
10. removeAll(Collection<?> c): Removes all of the elements in the specified collection from this collection.
11. retainAll(Collection<?> c): Retains only the elements in this collection that are contained in the specified collection.
12. containsAll(Collection<?> c): Returns true if this collection contains all of the elements in the specified collection.
Note that not all Collection interfaces contain all of these methods - some interfaces may only contain a subset of these methods, or may contain additional methods that are specific to that interface.
Also, it's worth noting that some of these methods may have different implementations or performance characteristics depending on the specific Collection implementation being used. For example, adding an element to an ArrayList is typically faster than adding an element to a LinkedList, but removing an element from the middle of an ArrayList is slower than removing an element from a LinkedList. We will discuss it letter in details.
Next We will see details definition and how can be handled, one by one every interfaces and their implementation classes.