Java Collections Framework

  • In Java, the java.util package contains a number of classes and interfaces that are collectively referred to as the Java Collections Framework.
  • The Collections Framework provides a unified architecture for storing, managing, and manipulating collections of objects in Java.

There are several types of collections in Java, including:

1. Lists:

A list is an ordered collection of elements that can contain duplicate values. Examples of lists include ArrayList and LinkedList. Here’s an example of how you could use an ArrayList to store information about cars:

There are majorly 3 type of list and their difference is stated below:

FeatureArrayListVectorLinkedList
ImplementationDynamic ArrayDynamic ArrayDoubly-Linked List
Thread SafetyNot thread-safeThread-safeNot thread-safe
PerformanceFaster than VectorSlower than ArrayListFaster for insertion and deletion; slower for accessing elements by index
ResizeResizes dynamicallyResizes dynamicallyN/A (no index access)
Methodsadd(), remove(), get(), set()addElement(), removeElement(), elementAt(), setElementAt()addFirst(), addLast(), removeFirst(), removeLast(), getFirst(), getLast()

2. Sets:

A set is an unordered collection of unique elements. Examples of sets include HashSet and TreeSet. Here’s an example of how you could use a HashSet to store information about cars:

There are various type of sets , below are the difference:

In Java, there are several implementations of the Set interface, including HashSet, TreeSet, and LinkedHashSet. Here’s a comparison of these set implementations in tabular format:

FeatureHashSetTreeSetLinkedHashSet
ImplementationHash tableRed-black treeHash table with linked list
Thread SafetyNot thread-safeThread-safe (for individual operations)Not thread-safe
Null ElementsAllows one null elementAllows one null elementAllows one null element
OrderingNo guaranteed orderSorted in ascending order of elementsInsertion order
PerformanceFast for adding and checking for containment; slower for searching and removingSlow for adding and removing; fast for searching and checking for containmentFast for adding and checking for containment; slower for searching and removing

Note: HashSet provides fast add and check for containment operations, but slower search and removal operations. TreeSet provides sorted elements, but slower add and remove operations. LinkedHashSet provides elements in the order they were added, but slower search and removal operations compared to HashSet.

3. Maps:

A map is a collection of key-value pairs where each key is unique. In Java, the java.util package provides several classes and interfaces to implement maps, such as HashMap and TreeMap. A HashMap is an implementation of a hash table, while a TreeMap is an implementation of a balanced tree.

Here’s an example of how you could use a HashMap to store information about cars, where the key is the car’s make and the value is the car object:

In this example, the cars map is created as an instance of HashMap<String, Car>, where the String type is used as the key and the Car type is used as the value. The put method is used to add key-value pairs to the map, and the keySet method is used to obtain a set of all the keys in the map. The for loop iterates over the keys in the set, and for each iteration, it prints a string that includes the current car’s information, which is obtained by calling the get method with the current key.

several type of Map and their differences:

In Java, there are several implementations of the Map interface, including HashMap, TreeMap, Hashtable, and LinkedHashMap. Here’s a comparison of these map implementations in tabular format:

FeatureHashMapTreeMapHashtableLinkedHashMap
ImplementationHash tableRed-black treeHash tableHash table with linked list
Thread SafetyNot thread-safeThread-safe (for individual operations)Thread-safeNot thread-safe
Null KeysAllows one null keyAllows one null keyDoes not allow null keysAllows one null key
Null ValuesAllows multiple null valuesAllows multiple null valuesDoes not allow null valuesAllows multiple null values
OrderingNo guaranteed orderSorted in ascending order of keysNo guaranteed orderInsertion order
PerformanceFast for adding and checking for containment; slower for searching and removingSlow for adding and removing; fast for searching and checking for containmentSlower than HashMapFast for adding and checking for containment; slower for searching and removing

4. Queue:

it follow FIFO(first in first out by default)

FeaturePriorityQueueArrayDeque
ImplementationHeapResizable array
Thread SafetyNot thread-safeNot thread-safe
Null ElementsDoes not allow null elementsAllows multiple null elements
OrderingSorted in ascending order of elementsNo guaranteed order
PerformanceFast for adding and removing elements from the front, but slower for adding and removing elements from the middle or endFast for adding and removing elements from the front and end, but slower for adding and removing elements from the middle

example code:

output:

First car in the queue: Ford Mustang
First car in the queue: Honda Accord

In this example, we’ve used a custom Car class that implements the Comparable interface, so that we can specify the order in which cars are stored in the priority queue. The cars are stored in ascending order of their make, so the car with the make “Ford” is the first car in the queue.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top