Java Collections Framework
A collection is a group of objects contained in a single object. All collections framework classes contained inside java.util package.
There are 4 main interfaces for this framework:
List: A list is an ordered collection of elements that allows duplicate entries. Elements of list can be accessed using int index.
Set: A set is a collection that does not allow duplicate entries.
Queue: A queue is a collection that orders its elements in a specific order for processing. A typical queue processes its elements in a first-in, first-out order, but other orderings are possible.
Map: A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs.
The Collection interface is the root of all collections except maps.
Notice that Map doesn’t implement the Collection interface. It is considered part of the Java Collections Framework, even though it isn’t technically a Collection. It is a collection (note the lowercase), though, in that it contains a group of objects. The reason why maps are treated differently is that they need different methods due to being key/value pairs.
List interface
Lists are commonly used because there are many situations in programming where you need to keep track of a list of objects. Unlike an array, though, many List implementations can change in size after they are declared.
How to create List:
List<String> list = new ArrayList<>();
list.add("SD"); // [SD]
list.add(0, "NY"); // [NY,SD]
list.set(1, "FL"); // [NY,FL]
System.out.println(list.get(0)); // NY
list.remove("NY"); // [FL]
list.remove(0); // []
While the classes implementing the List interface have many methods, you need to know only the most common ones. The main thing that all List implementations have in common is that they are ordered and allow duplicates.
Set Interface
We use a set when we don’t want to allow duplicate entries. For example, you might want to keep track of the unique emails that you want to use for sending emails for different people.
How to create Set:
Set<Integer> set = new HashSet<>();
boolean b1 = set.add(66); // true
boolean b2 = set.add(10); // true
boolean b3 = set.add(66); // false
boolean b4 = set.add(8); // true
set.forEach(System.out::println);
Queue Interface
You use a queue when elements are added and removed in a specific order. Queues are typically used for sorting elements prior to processing them.
How to create Queue:
Queue<Integer> queue = new LinkedList<>();
System.out.println(queue.offer(10)); // true
System.out.println(queue.offer(4)); // true
System.out.println(queue.peek()); // 10
System.out.println(queue.poll()); // 10
System.out.println(queue.poll()); // 4
System.out.println(queue.peek()); // null
Unless stated otherwise, a queue is assumed to be FIFO (first-in, first-out).
As we can see in the general diagram above, LinkedList belongs both List and Queue interfaces. In addition to being a list, it is a double-ended queue. A double-ended queue is different from a regular queue in that you can insert and remove elements from both the front and back of the queue.
Map Interface
You use a map when you want to identify values by a key. For example, when you use the contact list in your phone, you look up “Name” rather than looking through each phone number in turn. The main thing that all Map classes have in common is that they all have keys and values.
How to create Map:
Map.of("key1", "value1", "key2", "value2");
// Or like below
Map.ofEntries(
Map.entry("key1", "value1"),
Map.entry("key1", "value1"));
That’s it for now, I think you get some knowledge about Collections framework :)
All information about this article is based on Java OCP 11 book by Selikoff and Jeanne