For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
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.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.