Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
Now Reading
12 mins read
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Java is one of the major programming languages, with more than 33.27% of users around the globe. Developed in 1991 by James Gosling of Sun Microsystems, Inc., this language quickly grew a user base and boasted a rich system.
Java collections offer a one-stop destination for all sorts of data manipulation tasks. It is an all the more perfect opportunity to start learning the language.
If you're looking at Java collection framework notes, you've landed at the right place. This tutorial will examine how to utilize best practices using the Java framework.
While working on the Java collection library, we'll employ the latest developments and ensure better learning of Java. Let's begin our journey into Java's collection framework in depth.
The Java collection framework is a set of interfaces and classes working to offer a unified front. This framework works towards storing and manipulating collections of objects.
One of the essentials of the Java language, it provides the interface for several data structures. These include lists, maps, sets, and queues. Classes involve ArrayList, PriorityQueue, TreeSet, Vector, and others.
A Java collection is a group of objects that represent a single unit. It is based on a framework that offers a network to store and manipulate data as a group of objects. Its features include:
The Java Collections Framework is a set of classes and interfaces containing popular data structures. It was introduced to allow coders to work with different types of collections depending on their immediate needs.
The Java collection framework is designed to carry out simple tasks such as sorting and searching. These include lists, sets, queues, deques, and maps. Much like the list interface, the map interface offers methods for adding, removing, and searching pairs.
It is a core portion of Java, and the 'Collection Framework' can be found in JDK 1.2.
As Java was still in its initial stages, the Java collections weren't accessible back in 1998. As the collection framework was incredibly complicated, and it was difficult in the first release. Additionally, the language designers wanted to test and debug before introducing complex features.
With the release of JDK 1.2 and Java.util package containing the tools, the Java Collection Framework was ready for users.
Collection | Collection Framework |
The collection is an interface. | The collections framework is a library. |
The collections interface represents a group of objects. | The collections framework consists of a set of classes and interfaces. |
The collections interface offers a basic set of operations such as add(), remove(), contains(), etc. | The collections framework offers a more complicated operations like sorting, shuffling, searching, and reversing. |
It can be used with a single object type. | The collections framework works with different types of objects. |
It provides fundamentals for manipulating content in a collection. | The collection framework deals with a list of advanced methods for content manipulation. |
The collection framework is a versatile library that offers the following advantages -
It maintains a set of interfaces like Set, Collection, Map, and List, along with the classes (ArrayList, Vector, LinkedList) for carrying out the implementations.
It encourages object-oriented programming or abstraction. Simultaneously, it can help them focus on utilizing the other parts of the program.
With the collections framework, a programmer doesn't have to consider implementing a data structure anymore. Instead, they can utilize the most effective performance, drastically improving speed and quality.
The collection framework is based on a hierarchical structure of classes and interfaces. It is organized into two branches - the map and the collection branches.
The map branch is divided into Mapo, SortedMap, and NavigableMap interfaces, which store key-value data.
The collection branch is divided into Set, List, and Queue interfaces, which store data similarly.
There are specialized collection classes like Vector and Hashtable, which provide high-performance implementations.
Some standard collection interfaces are:
1. add() - It adds an object to the existing collection.
2. remove() - It adds elements in the existing collection to the collection.
3. contains() - This method determines whether the collection contains an element.
4. size() - It returns the total number of elements in the collection.
5. spiterator() - It creates a Spliterator over the elements.
6. toArray() -It returns an array of all collection elements.
7. clear() - It removes every element from the collection.
8. stream() - It returns a sequential stream as a source.
9. retainAll() - It retains only specific elements in a collection.
10. removeAll() - It removes every oa collection.
11. equals() - It compares specified objects based on equality.
12. hashCode() - It returns the hash code for each collection.
An iterable interface is an object where the elements can be accessed sequentially.
Iterator iterator();
A collection interface is an object divided into an array, set, list, or Map.
interface Collection<E>
The list interface is an abstract data type specifying listicle operations.
interface [interface-name] {
[interface-properties]
};
The collection framework works with several classes, which are demonstrated below:
1. ArrayList
The ArrayList works like an array. It can dynamically grow and shrink per the requirement of the programmer.
import java.util.*;
class MyArrayList {
public static void main(String[] args) {
ArrayList<String> myStrings = new ArrayList<String>(); // Creating ArrayList
myStrings.add("Alice"); // Adding object in ArrayList
myStrings.add("Roland");
myStrings.add("Anny");
myStrings.add("Charlie");
// Traversing ArrayList through Iterator
Iterator<String> iterator = myStrings.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
2. LinkedList
The LinkedList stores data linearly. It works with the help of nodes connected like pointers and can store a large amount of data.
import java.util.*;
public class MyLinkedList {
public static void main(String[] args) {
LinkedList<String> myStrings = new LinkedList<String>(); // Creating LinkedList
myStrings.add("Brianne"); // Adding object in LinkedList
myStrings.add("Bart");
myStrings.add("Archie");
myStrings.add("Garry");
Iterator<String> iterator = myStrings.iterator(); // Traversing LinkedList through Iterator
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
3. Vector
Vector allows for storage and retrieval of data in a compact form.
import java.util.*;
public class MyVector {
public static void main(String[] args) {
Vector<String> myStrings = new Vector<String>(); // Creating Vector
myStrings.add("Mert"); // Adding object in Vector
myStrings.add("Ashley");
myStrings.add("Bob");
myStrings.add("Barry");
Iterator<String> iterator = myStrings.iterator(); // Traversing Vector through Iterator
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
4. Stack
Stack comes under Vector and a legacy class and employs the last-in-first-out concept.
import java.util.*;
public class MyStack {
public static void main(String[] args) {
Stack<String> myStack = new Stack<String>(); // Creating Stack
myStack.push("Tina"); // Adding object in Stack
myStack.push("Alexa");
myStack.push("Mary");
myStack.push("John");
myStack.pop();
Iterator<String> iterator = myStack.iterator(); // Traversing Stack through Iterator
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
5. Queue Interface
The queue interface is also based on the FIFO principle and is akin to a real-world queue line.
Queue<String> q1 = new PriorityQueue();
Queue<String> q2 = new ArrayDeque();
a) Priority Queue
A priority queue is a type of data structure in which each element has priority.
import java.util.*;
public class MyPriorityQueue {
public static void main(String[] args) {
PriorityQueue<String> myQueue = new PriorityQueue<String>(); // Creating PriorityQueue
myQueue.add("Kim"); // Adding object in PriorityQueue
myQueue.add("Jimmy");
myQueue.add("Jack");
myQueue.add("George");
System.out.println("head:"+myQueue.element());
System.out.println("head:"+myQueue.peek());
System.out.println("iterating the queue elements:");
Iterator<String> iterator = myQueue.iterator(); // Traversing PriorityQueue through Iterator
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
myQueue.remove();
myQueue.poll();
System.out.println("after removing two elements:");
Iterator<String> iterator2 = myQueue.iterator();
while (iterator2.hasNext()) {
System.out.println(iterator2.next());
}
}
}
6. Deque Interface
The deque interface allows for easy adding and removal of elements from both ends of a queue.
Deque d = new ArrayDeque();
a) Array Deque
An array queue stores objects in a double-ended queue or deque.
import java.util.*;
public class MyDeque {
public static void main(String[] args) {
Deque<String> myDeque = new ArrayDeque<String>(); // Creating Deque and adding elements
myDeque.add("Jenny");
myDeque.add("Kate");
myDeque.add("Alex");
// Traversing elements
for (String str : myDeque) {
System.out.println(str);
}
}
}
7. Set Interface
A set interface is an unordered collection of objects that does not allow duplicate values to be stored.
Set<data-type> s1 = new HashSet<data-type>();
Set<data-type> s2 = new LinkedHashSet<data-type>();
Set<data-type> s3 = new TreeSet<data-type>();
The following classes implement the Set interface:
1. Hash Set
A hash set is a type of data structure that stores and retrieves data in an unorganized manner. It does not maintain an ordering over its elements.
import java.util.*;
public class TestJavaCollection7{
public static void main(String args[]){
//Creating HashSet and adding elements
HashSet<String> set=new HashSet<String>();
set.add("Stella");
set.add("Robert");
set.add("Kevin");
set.add("Ben");
//Traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
2. Linked Hash Set
The LinkedHashSet has the same characteristics as a HashSet. However, it is different because it uses a doubly linked list to store data and retain elements.
import java.util.*;
public class MyLinkedHashSet {
public static void main(String[] args) {
LinkedHashSet<String> mySet = new LinkedHashSet<String>(); // Creating LinkedHashSet
mySet.add("Arun"); // Adding elements in LinkedHashSet
mySet.add("Vineet");
mySet.add("Abhishek");
mySet.add("Sapna");
Iterator<String> iterator = mySet.iterator(); // Traversing LinkedHashSet through Iterator
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
3. Sorted Set Interface
A set interface provides a set of methods for operations on a set for sorting a specific order.
SortedSet<data-type> set = new TreeSet();
The class implementing the sorted set interface is TreeSet.
i) Tree Set
A TreeSet is a collection of elements in a tree-like structure. It is similar to a HashSet, but it stores elements ascendingly.
import java.util.*;
public class TestJavaCollection9 {
public static void main(String args[]) {
// Creating and adding elements
TreeSet<String> set = new TreeSet<String>();
set.add("Chris");
set.add("Raj");
set.add("Albin");
set.add("Rahul");
// Traversing elements
Iterator<String> itr = set.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
4. Map Interface
The map interface utilizes an object that maps keys to values. It isn't a subtype of the collection interface.
The frequently used implementation of a Map interface is a HashMap.
i) Hash Map
HashMap utilizes a hash table for storing data. It holds key-value pairs and uses a hash key for accessing the element.
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
// Create a hash map
HashMap<String, Double> accountBalances = new HashMap<String, Double>();
// Put elements to the map
accountBalances.put("Akash", new Double(3434.34));
accountBalances.put("Manish", new Double(123.22));
accountBalances.put("Arya", new Double(1378.00));
accountBalances.put("Suman", new Double(99.22));
accountBalances.put("Zainab", new Double(-19.08));
// Get a set of the entries
Set<Map.Entry<String, Double>> entries = accountBalances.entrySet();
// Get an iterator
Iterator<Map.Entry<String, Double>> iterator = entries.iterator();
// Display elements
while(iterator.hasNext()) {
Map.Entry<String, Double> entry = iterator.next();
System.out.print(entry.getKey() + ": ");
System.out.println(entry.getValue());
}
System.out.println();
// Deposit 1000 into Akash account
double balance = accountBalances.get("Akash").doubleValue();
accountBalances.put("Akash", new Double(balance + 1000));
System.out.println("Akash’s new balance: " + accountBalances.get("Akash"));
}
}
The Java collections are an invaluable tool for developers. They are helpful as they offer a powerful and efficient way of managing data collection.
Java collection's multi-level approach helps developers save time and effort when programming. It consists of multiple valuable data structures that can store and manage data according to a programmer's needs.
Knowing Java collections is essential for a developer who wants to make the most out of their code while, at the same time, also saving up on time.
1. Which is the fastest collection in the Java collections framework?
The HashSet collection is the fastest in Java. In the same way, the LinkedHashSet collection is an excellent asset to developers looking to insert the order of elements.
2. How is a collection different from collections?
A collection is a unified interface, whereas collections form a class of objects.
3. Which are the top collection types in the Java framework?
The linear format, namely ArrayList and LinkedList, are the most comprehensive collection types in the Java framework.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
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...