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
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. 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
Disclaimer
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.