- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.ArrayList; class Car { String make; String model; int year; // constructor and other methods } class Main { public static void main(String[] args) { ArrayList<Car> cars = new ArrayList<Car>(); cars.add(new Car("Toyota", "Camry", 2020)); cars.add(new Car("Honda", "Accord", 2019)); cars.add(new Car("Tesla", "Model 3", 2018)); for (int i = 0; i < cars.size(); i++) { System.out.println("Car " + (i + 1) + ": " + cars.get(i).make + " " + cars.get(i).model + " " + cars.get(i).year); } } } |
There are majorly 3 type of list and their difference is stated below:
Feature | ArrayList | Vector | LinkedList |
---|---|---|---|
Implementation | Dynamic Array | Dynamic Array | Doubly-Linked List |
Thread Safety | Not thread-safe | Thread-safe | Not thread-safe |
Performance | Faster than Vector | Slower than ArrayList | Faster for insertion and deletion; slower for accessing elements by index |
Resize | Resizes dynamically | Resizes dynamically | N/A (no index access) |
Methods | add(), 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.HashSet; class Car { String make; String model; int year; // constructor and other methods } class Main { public static void main(String[] args) { HashSet<Car> cars = new HashSet<Car>(); cars.add(new Car("Toyota", "Camry", 2020)); cars.add(new Car("Honda", "Accord", 2019)); cars.add(new Car("Tesla", "Model 3", 2018)); for (Car car : cars) { System.out.println("Car: " + car.make + " " + car.model + " " + car.year); } } } |
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:
Feature | HashSet | TreeSet | LinkedHashSet |
---|---|---|---|
Implementation | Hash table | Red-black tree | Hash table with linked list |
Thread Safety | Not thread-safe | Thread-safe (for individual operations) | Not thread-safe |
Null Elements | Allows one null element | Allows one null element | Allows one null element |
Ordering | No guaranteed order | Sorted in ascending order of elements | Insertion order |
Performance | Fast for adding and checking for containment; slower for searching and removing | Slow for adding and removing; fast for searching and checking for containment | Fast 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.HashMap; class Car { String make; String model; int year; // constructor and other methods } class Main { public static void main(String[] args) { HashMap<String, Car> cars = new HashMap<String, Car>(); cars.put("Toyota", new Car("Toyota", "Camry", 2020)); cars.put("Honda", new Car("Honda", "Accord", 2019)); cars.put("Tesla", new Car("Tesla", "Model 3", 2018)); for (String make : cars.keySet()) { System.out.println("Car: " + cars.get(make).make + " " + cars.get(make).model + " " + cars.get(make).year); } } } |
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:
Feature | HashMap | TreeMap | Hashtable | LinkedHashMap |
---|---|---|---|---|
Implementation | Hash table | Red-black tree | Hash table | Hash table with linked list |
Thread Safety | Not thread-safe | Thread-safe (for individual operations) | Thread-safe | Not thread-safe |
Null Keys | Allows one null key | Allows one null key | Does not allow null keys | Allows one null key |
Null Values | Allows multiple null values | Allows multiple null values | Does not allow null values | Allows multiple null values |
Ordering | No guaranteed order | Sorted in ascending order of keys | No guaranteed order | Insertion order |
Performance | Fast for adding and checking for containment; slower for searching and removing | Slow for adding and removing; fast for searching and checking for containment | Slower than HashMap | Fast for adding and checking for containment; slower for searching and removing |
4. Queue:
it follow FIFO(first in first out by default)
Feature | PriorityQueue | ArrayDeque |
---|---|---|
Implementation | Heap | Resizable array |
Thread Safety | Not thread-safe | Not thread-safe |
Null Elements | Does not allow null elements | Allows multiple null elements |
Ordering | Sorted in ascending order of elements | No guaranteed order |
Performance | Fast for adding and removing elements from the front, but slower for adding and removing elements from the middle or end | Fast for adding and removing elements from the front and end, but slower for adding and removing elements from the middle |
example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class Car implements Comparable<Car> { private String make; private String model; public Car(String make, String model) { this.make = make; this.model = model; } public String getMake() { return make; } public String getModel() { return model; } @Override public int compareTo(Car other) { return make.compareTo(other.make); } @Override public String toString() { return make + " " + model; } } public class CarPriorityQueue { public static void main(String[] args) { PriorityQueue<Car> queue = new PriorityQueue<>(); // Add cars to the queue queue.add(new Car("Toyota", "Camry")); queue.add(new Car("Honda", "Accord")); queue.add(new Car("Ford", "Mustang")); // Check the first car in the queue System.out.println("First car in the queue: " + queue.peek()); // Remove the first car from the queue queue.remove(); // Check the new first car in the queue System.out.println("First car in the queue: " + queue.peek()); } } |
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.