Engineering Full Stack Apps with Java and JavaScript
Collection framework in Java provides a set of readily usable efficient implementations for commonly used data structures and algorithms.
A basic understanding of the data constructs like arrays, String, linked list, stack, queue, trees etc. and concepts like sorting, searching, hashing etc. from computer science will help you understand collections better.
We can use and reuse these implementations of data structures and algorithms in a type safe manner, using it along with Generics. While Java collections is a framework and not a language feature, generics is a language feature introduced in Java 1.5.
Some collections allow duplicate elements and some collections do not allow. Some collections are ordered and some collections are unordered.
We can extend these collections and add our on implementation through inheritance; collections framework provide many interfaces and abstract classes for this purpose.
Collections API mainly contains below three types of components:
Abstractions (abstract classes and interfaces)
We can extend collections framework and add our on implementation through inheritance, from these abstractions.
Examples include Iterable, Collection, List, Set, SortedSet, NavigableSet, Queue, Deque, Map, SortedMap, NavigableMap, Iterator, ListIterator etc.
Concrete classes
These are the actual container implementations (often based on abstractions) that we can actually use in our programs.
Examples include ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, PriorityQueue etc.
Algorithms
These include commonly used functionality such as sorting, searching etc.
Algorithms are mostly part of the java.util.Collections util class.
Example methods in java.util.Collections interface that acts on various collections include binarySearch, copy, fill, max, min, replaceAll, reverse, repeat, shuffle, sort and swap.
java.util.Collections class is an utility class with many methods that can be applied to various collections.
java.util.Collection interface is the parent interface for many collections such as List, Set, Queue etc.
The collection, with small c, denotes the collections framework (the concept).
Iterable interface is a parent of Collection interface.
Most collection interfaces such as List, Set and Queue are children of Collection interface, but Map interface is not.
The Map interface is the parent interface in the map hierarchy. SortedMap interface is a child of Map interface and NavigableMap interface is a child of SortedMap interface.
There are various implementations, sub interfaces, abstract implementations of List, Set, Queue and Map in the Java collections framework.
List interface
List is an ordered collection.
Set interface
Set is a collection that does not contain duplicates.
Map interface
Map is a group of key value pairs.
Tree (data structure concept)
Tree based data structures are sorted and ordered.
There is no Tree interface or Tree class in Java, but there are Map and Set implementations such as TreeMap and TreeSet that uses the concept of Trees.
Queue interface
Queue processes its elements in the order "First In First Out" (e.g. queue in a movie theatre).
Queue is often compared against Stack data structure, and a Stack processes its elements in the order "Last in First Out".
Stack is a class in Java unlike Queue which is an interface. Stack is implemented as a child of List in Java.
Deque interface
Deque stands for Double Ended Queue
Allow you to insert and remove elements from both the ends.
BlockingDeque interface is a child of Deque interface.
ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque and a LinkedList implement Deque interface.