Java Collections
Java Collections
Java
Collections
Hierarchy of Collection Framework @techwithvishalraj
@techwithvishalraj
Collections in Java
Java Collections can achieve all the operations that you perform on a
data such as searching, sorting, insertion, manipulation and deletion.
The java.util package contains all the classes and interfaces for the
Collection framework except Iterable interface which is present in
java.lang package.
List Interface
@techwithvishalraj
Map Interface
A map contains key and value pair. Each key and value pair is known as an
entry. A Map contains unique keys.
A Map is useful if you have to search, update or delete elements on the basis
of a key.
A Map doesn't allow duplicate keys, but you can have duplicate values.
HashMap and LinkedHashMap allow null keys and values, but TreeMap
doesn't allow any null key or value.
Queue Interface
A queue is a linear data structure that follows the "first-in, first-out" (FIFO)
principle.
It is an ordered list of objects.
list.add(1);
list.add(5);
list.size() ----> 2
list.isEmpty() ----> false
list.remove(1) ----> 1 is the index
list.toString() ----> [1, 5]
list.addAll(list1);
LinkedList Class
@techwithvishalraj
LinkedList is a class that implements the List interface and is used to create
and manipulate doubly-linked lists.
A doubly-linked list is a data structure in which each element is a node that
contains a reference to both the next and previous nodes in the list.
linkedList.add("First");
linkedList.addLast("Last");
linkedList.addFirst("NewFirst");
linkedList.remove("First");
linkedList.removeFirst();
linkedList.removeLast();
stack.push(10);
stack.push(20);
stack.push(30);
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(1, 25);
numbers.remove(2);
numbers.clear();
Elements with higher priority are dequeued before elements with lower priority.
The priority queue processes elements in ascending order by default.
priorityQueue.add(5);
priorityQueue.add(2);
priorityQueue.add(8);
priorityQueue.add(1);
deque.addFirst(1);
deque.addLast(2);
deque.offerFirst(4);
deque.offerLast(3);
Note:
addFirst() may throw an exception if the deque is full.
offerFirst() returns false if the element cannot be added due to
capacity constraints but does not throw an exception.
HashSet Class
@techwithvishalraj
set.add("Apple");
set.add("Banana");
set.add("Cherry");
set.remove("Banana");
set.clear();
LinkedHashSet Class @techwithvishalraj
set.add("Apple");
set.add("Banana");
set.add("Cherry");
set.remove("Banana");
numbers.add(5);
numbers.add(2);
numbers.add(8);
numbers.add(1);
numbers.add(4);
treeMap.put(3, "Three");
treeMap.put(1, "One");
treeMap.put(2, "Two");
treeMap.remove(1);
int size = treeMap.size(); -------> 2
treeMap.isEmpty();
treeMap.clear();
HashMap Class
@techwithvishalraj
scores.put("Alice", 95);
scores.put("Bob", 88);
scores.put("Charlie", 92);
scores.remove("Charlie");
In LinkedHashMap, the elements are returned in the order in which they were
added to the map or in the order in which they were most recently accessed,
depending on the constructor used.
keys must be unique, values can be duplicated.
It allows null keys and values.
list.add(1);
list.add(6);
list.add(0);
Collections.addAll(list, 8, 3);
Integer[] arr = {5, 4, 1};
Collections.addAll(list, arr);
System.out.println(list);
int max=Collections.max(list);
int min=Collections.min(list);
int frequency=Collections.frequency(list, 1);
int index = Collections.binarySearch(list, 4);
System.out.println(max+" "+ min +" "+ frequency+" "+ index);
Collections.reverse(list);
System.out.println(list);
Thank
you!
EXPLORE MORE
@techwithvishalraj