Tutorial Playlist
Collections in Java are objects They serve as a unified entity to hold and manage a group of objects. These are referred to as its elements. It provides a standardized approach for handling objects within a class. It does so by treating them as a cohesive unit.
This tutorial elaborates on the various aspects of collections in Java, including answers to standard questions such as “What is a collection in Java?”, “What is a framework in Java?” and “Hierarchy of collection.”
Read on to learn more.
In real-life scenarios, collections in Java can be compared to various everyday situations to understand their functionality:
A framework in Java is a reusable and pre-designed structure. This provides a foundation for developing applications. It offers pre-defined functions, classes, and libraries. These simplify and accelerate the development process.
The collections framework in Java is a comprehensive architecture. It is designed to simplify the representation and manipulation of collections. It consists of a hierarchy of interfaces and classes. This is within the java.util package. This makes it a widely used and powerful tool in Java programming.
The collections framework streamlined the handling of collections in Java. This is primarily due to its common interface and interconnection between various collection types. It offers a range of interfaces, classes, and algorithms. They facilitate efficient storage and manipulation of groups of objects. As a result, it enhances the productivity and effectiveness of Java developers.
The collections framework in Java is structured in a hierarchy. It encompasses classes and interfaces found in the java.util package.
Let's clarify some key terms so that we can understand the hierarchy better:
This overview table summarizes the key methods for each component and includes examples to demonstrate their usage.
Component | Key Methods | Examples |
Iterator Interface | hasNext(), next(), remove() | Iterator<String> it = list.iterator(); |
Iterable Interface | iterator() | Iterable<String> iterable = list; |
Collection Interface | add(), remove(), contains(), size(), clear() | Collection<String> coll = new ArrayList<>(); |
List Interface | add(), get(), remove(), set(), size() | List<String> list = new ArrayList<>(); |
ArrayList | add(), get(), remove(), size(), ensureCapacity() | ArrayList<Integer> list = new ArrayList<>(); |
LinkedList | add(), get(), remove(), addFirst(), addLast() | LinkedList<String> list = new LinkedList<>(); |
Vector | add(), get(), remove(), size(), capacity() | Vector<String> vector = new Vector<>(); |
Stack | push(), pop(), peek(), empty() | Stack<Integer> stack = new Stack<>(); |
Queue Interface | add(), remove(), peek(), offer() | Queue<String> queue = new LinkedList<>(); |
PriorityQueue | add(), peek(), poll(), offer() | PriorityQueue<Integer> pq = new PriorityQueue<>(); |
Deque Interface | addFirst(), addLast(), removeFirst(), removeLast() | Deque<String> deque = new ArrayDeque<>(); |
ArrayDeque | addFirst(), addLast(), removeFirst(), removeLast() | ArrayDeque<String> deque = new ArrayDeque<>(); |
Set Interface | add(), remove(), contains(), size() | Set<String> set = new HashSet<>(); |
HashSet | add(), remove(), contains(), size() | HashSet<String> set = new HashSet<>(); |
LinkedHashSet | add(), remove(), contains(), size() | LinkedHashSet<String> set = new LinkedHashSet<>(); |
SortedSet Interface | first(), last(), subSet(), headSet(), tailSet() | SortedSet<String> sortedSet = new TreeSet<>(); |
TreeSet | add(), remove(), first(), last(), headSet() | TreeSet<String> treeSet = new TreeSet<>(); |
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class upGradTutorials {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
iterator.remove();
}
}
}
Syntax:
import java.util.ArrayList;
import java.util.List;
public class upGradTutorials {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
for (Integer element : list) {
System.out.println(element);
}
}
}
Syntax:
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class upGradTutorials {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.remove("Java");
System.out.println(list.contains("Python"));
}
}
Syntax:
import java.util.ArrayList;
import java.util.List;
public class upGradTutorials {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add(0, "Banana");
System.out.println(list.get(1));
System.out.println(list.size());
}
}
Syntax:
import java.util.ArrayList;
import java.util.List;
public class upGradTutorials {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
System.out.println(list);
}
}
Syntax:
import java.util.LinkedList;
import java.util.List;
public class upGradTutorials {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
System.out.println(list);
}
}
Syntax:
import java.util.List;
import java.util.Vector;
public class upGradTutorials {
public static void main(String[] args) {
List<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Orange");
System.out.println(vector);
}
}
Syntax:
import java.util.Stack;
public class upGradTutorials {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Apple");
stack.push("Banana");
stack.push("Orange");
System.out.println(stack.pop());
}
}
Syntax:
import java.util.LinkedList;
import java.util.Queue;
public class upGradTutorials {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Apple");
queue.offer("Banana");
queue.offer("Orange");
System.out.println(queue.poll());
System.out.println(queue.peek());
}
}
Syntax:
import java.util.PriorityQueue;
import java.util.Queue;
public class upGradTutorials {
public static void main(String[] args) {
Queue<Integer> queue = new PriorityQueue<>();
queue.offer(5);
queue.offer(2);
queue.offer(8);
System.out.println(queue.poll()); // Output: 2
System.out.println(queue.peek()); // Output: 5
}
}
Syntax:
import java.util.ArrayDeque;
import java.util.Deque;
public class upGradTutorials {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("Apple");
deque.addLast("Banana");
deque.addLast("Orange");
System.out.println(deque.pollFirst());
System.out.println(deque.peekLast());
}
}
Syntax:
import java.util.ArrayDeque;
import java.util.Deque;
public class upGradTutorials {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("Apple");
deque.add("Banana");
deque.add("Orange");
System.out.println(deque);
}
}
Syntax:
import java.util.HashSet;
import java.util.Set;
public class upGradTutorials {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
System.out.println(set);
}
}
Syntax:
import java.util.HashSet;
import java.util.Set;
public class upGradTutorials {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
System.out.println(set);
}
}
Syntax:
import java.util.LinkedHashSet;
import java.util.Set;
public class upGradTutorials {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
System.out.println(set);
}
}
Syntax:
import java.util.SortedSet;
import java.util.TreeSet;
public class upGradTutorials {
public static void main(String[] args) {
SortedSet<String> set = new TreeSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
System.out.println(set);
}
}
Syntax:
import java.util.SortedSet;
import java.util.TreeSet;
public class upGradTutorials {
public static void main(String[] args) {
SortedSet<String> set = new TreeSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
System.out.println(set);
}
}
Syntax:
Before the collections framework was introduced, Java had individual classes. There were Vectors, Stacks, and Arrays. These were each with its own set of methods and functionality. This lack of standardization and uniformity made it challenging for developers to work with collections and limited code reusability.
The collections framework addresses these issues by providing a unified architecture with consistent interfaces and classes. It standardized how collections are represented, manipulated, and accessed, making it easier for developers to work with collections and enabling code reuse across different collection types.
Additionally, the framework offers high-performance implementations of various data structures and algorithms, improving program speed and quality. Overall, the collections framework in Java greatly simplifies and enhances collection handling in programming.
The collection framework in Java offers several advantages:
The choice of collection framework depends on the program's specific requirements.
Collections in Java give us a powerful set of tools to manage groups of objects. This helps us work with complex structures of data. If you aspire to be one, you could also enroll in a credible online software development course. Learning platforms such as upGrad have numerous software and web development courses to help you become a successful professional.
1. How is a map different from a collection?
A map is an interface that represents a mapping between keys and values. It is not considered a subtype of collection because it does not directly store objects. Map implementations, such as HashMap and TreeMap, store key-value pairs, allowing efficient lookup and retrieval based on keys.
2. Can I create my own custom collection classes in Java?
Yes, Java allows you to create your own custom collection classes by implementing the appropriate interfaces from the collection framework. This allows you to tailor collections to specific requirements and define custom behaviors and operations.
3. Is it possible to modify a collection while iterating over it?
In general, modifying a collection while iterating over it is not recommended using the enhanced for loop or an iterator. Doing so may result in a ConcurrentModificationException. Instead, use explicit methods like add, remove, or use the Iterator's remove method to modify the collection safely during iteration.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...