The Java Collections Framework is a unified set of classes and interfaces defined in the java.util package for storing collections. At the other side, a collection is a group of objects contained in a single element.
To represent the various type of collections, the java.util package contains a group of interfaces (e.g: collections interfaces) as follows:
A list is an ordered collection of elements that allows duplicate entries. Lists implements the List interface, and elements in a List can be accessed by an integer index. Lists are commonly used because there are many situations in programming where the developer needs to keep track of a list of objects. The List classes are: ArrayList, LinkedList, Vector and Stack.
The basic operations of List include the ability to add a single element at a specified index, add a collection of elements, replace or remove a specific element, and retrieve an element at a specific index.
ArrayList vs LinkedList
If the ArrayList were always better, then Java would give you only ArrayList. Same, if the LinkedList were always good, then Java would gibe you only LinkedList.
The both stretch as the developer adds the elements to them, and both gives the same results for the same operations (however, they differ in the term of performance). ArrayList are good for some operations, while LinkedList are good for other operations.
Operations that can be perform with ArrayList and LikedList:
- insertion at the beginning of the list;
- insertion at the end of the list;
- access the middle element.
A set is similar to a list, with the differences that a set collection does not allow duplicate entries, and it doesn’t have an index. Sets implement the Set interface.
The implementing classes of Set are: HashSet, LinkedHashSet and TreeSet. The basic operations of Set includes the ability to add or remove a single element or collection of elements, and to perform unions and intersections.
A queue is a collection that orders its element in a specific order for processing. A typical queue processes its elements in a FIFO way, but other ordering is possible. Queues implements the QueueInterface. The Queue classes are: PriorityQueue, LinkedList and ArrayDeque.
The basic operations of Queue include adding a single element, polling the queue to retrieve the next element, or peeking at the queue to see it there is a element available in the queue. The Deque operations are similar except elements can be added, polled, or peeked at both the beginning and end of the deque.
A map is a collection that maps key to value, with no duplicate keys allowed. The elements in a map are key-values pairs. Maps implements the Map interface.
Good to know : The Map interface is not a sub-interface of Collection like other types of collections. This makes the Map interface to be unique in a certain way.
The implementing classes of the Map interface are:
- HashMap (there is no ordering to the elements, and they are placed in the hash table based on their hashCode);
- LinkedHashMap (this type of Map stores its elements in a hash table and doubly linked lists. The linked list provides an ordering to the elements);
- TreeMap (stores its elements in a tree data structure with a user-defined or a natural order).
The idea behind MAPs is to provide a simple lookup table.