![]() |
VOOZH | about |
The Java Collection framework is a fundamental part of the Java programming language, It covers a major part of Java and acts as a prerequisite for many Advanced Java Topics.
Table of Content
Collection Framework in Java contains a Set of Interfaces and Classes to represent groups of objects as a Single Unit. In Java, the Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main โrootโ interfaces of Java collection classes.
Want to solidify your Java collection skills? Here's your chance! Get hands-on with these basic practice programs in Java Collection. Work with common collections like ArrayLists and HashSets, all while strengthening your Java coding abilities.
[Rahul, Utkarsh, Shubham, Neelam]
12345 After Performing Operation 1234 Set is also By Default Readable and Writable Exceptions is java.lang.UnsupportedOperationException true Now Set is Read-Only
Input : List = [3, 5, 18, 4, 6]
Output: Min value of our list : 3
Max value of our list : 18
Explanation: Comparison lead to result that the min of the list is 3 and max of list is 18. Similarly we can test for other Collections too.
Input: ArrayList : [10, 20, 30, 1, 2]
Index: 1
Output: ArrayList : [10, 30, 1, 2]
Explanation: Removed element from index 1.
Input: Linked_List: [geeks, for, geeks]
Output: 3
Explanation: As there are only 3 elements in the Collection So the value returned is 3.
ArrayList is a Collection which is the implementation of Dynamic Array Data Structure.
Input: Number of Elements to be Added: 3
Elements to be Added : "A" , "B" , "C"
Output: list=[ A, B, C ]
Input: ArrayList = [10, 20, 30, 1, 2] index=1
Output: [10, 30, 1, 2]
Input: ArrayList = [A, B, C, D]
index = 2
element =' E '
Output: [ A, B, E, D ]
Input: arr = [ 2, 3, 4, 5, 8]
Output: ArrayList = [ 2, 3, 4, 5, 8]
Input : ArrayList1 = [1, 2, 3, 4],
ArrayList2 = [4, 2, 3, 1]
Output: Array List are not equal
LinkedList Class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part.
Iterating the LinkedList using Iterator : 15 10 1 40 71
Input: List : {1,2,3,4}
Output: Set : {1,2,3,4}
Input: ArrayList : {1,2,3,4}
Output: LinkedList : {1,2,3,4}
Input: Array : {1,2,3,4}
Output: LinkedList : {1,2,3,4}
HashMap is the Class in Java Collection which is implemented based on Map Data Structure. It works on Key value pairs where we can store data in pairs , and can access based on Key values in Java.
Input: { "A" = "Angular" , "P": "Python" , "J" = "Java" , "H" = "Hibernate" }
Output: P = Python
A = Angular
H = Hibernate
J = Java
Input: HashMap: { 1=Geeks, 2=ForGeeks, 3=GeeksForGeeks }, key = 2
Output: True
Input: HashMap: { 1=G, 2=e, 3=e, 4=k, 5=s }, key = 10
Output: False
Input: "Alice is girl and Bob is boy"
Output: { Bob=1 , Alice=1 , and=1 , is=2 , girl=1 , boy=1 }
Input: HashMap1 = { A=1 , B=2 , C=3 }
Output: HashMap2 = { A=1 , B=2 , C=3 }
Input: str = "geeksforgeeks"
Output: s : 2
e : 4
g : 2
k : 2
Input: HashMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Output: TreeMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}
Challenge your understanding of Java HashSets! This section dives into practical problems that test your ability to leverage the unique properties of HashSets. Solve these problems and solidify your grasp of this essential Java collection!
Input: HashSet: [Geeks, For, ForGeeks, GeeksforGeeks]
Output: [For, ForGeeks, Geeks, GeeksforGeeks]
Input : Array: [1, 2, 3, 4, 5, 6]
Output: Set: [1, 2, 3, 4, 5, 6]
Input: Set: { 5 , 2 , 3 , 6 , null };
Output: null
2
3
5
6
Explanation: Elements of Set are not stored in the order they are inserted.
Stack is part of Collection in Java, it is a class which extends Vector class. It follows the principle of LIFO that is Last in First Out.
Input: stack = [10, 20, 30, 40, 50]
ele = 100
Output: [10 , 20 , 30 , 40 , 50, 100]
Explanation: Add element (ele) in the stack.
Input: S = โ(()(()))โ
Output: 6
Explanation:
() = 1 , then (()) =2
()(()) = 1+2 = 3
(()(())) = 2*3 = 6
Choose two adjacent characters, such that both are 1โs or 0โs. Then invert them, Formally, if both are 0โs then into 1โs or vice-versa.
If we are able to convert every element either to all 1 or all 0 then the answer will be Yes else No.
Input: N = 6, S = โ110110โ
Output: YES
Explanation: The operations are performed as:
Invert 0,1 -> S = 000110
Invert 4,5 -> S = 000000
Queue is the part of Java Collection, it is an Interface based on Queue Data Structure. It follows the Principle of FIFO that is First In First Out.
Below is the Solution of the question:
1 3 8 14 9 3
Input: List = { A, B, C}
Output: Queue = { A, B, C }
Got the hang of Java collections? Time to test your skills! This section throws some priority queue problems your way. Practice putting elements in order, taking them out strategically, and mastering this awesome data structure in a fun way.
Input: 5 , 1 , 10 , 30 , 20
Output: 1 , 5, 10 , 20 , 30
Explanation: Min Heap is a type of Data Structure in which internal node is smaller than or equal to its children.
PriorityQueue doesn't handle duplicate elements in a specific way. But we can configure PriorityQueue to add add duplicate elements in it.
Input: { 6 , 4 , 1 , 55 }
Output: Not Empty
Implementation of a PriorityQueue Using a Custom Comparator to Define the Priority of Elements in the Queue.
Input: items = [โLaptopโ, โTVโ, โPhoneโ, โWatchโ]
weights = [500, 1000,250, 50]
Output: [โTVโ, โLaptopโ, โPhoneโ, โWatchโ]
Explanation: Here the items are sorted based on their weight in decreasing order