Basic Program
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.
1. How to Print a Collection in Java?
// Java program to demonstrate how to
// Print a Collection
import java.util.*;
// Driver Class
class GFG {
// Main Function
public static void main(String[] args)
{
String[] geeks
= { "Rahul", "Utkarsh", "Shubham", "Neelam" };
List<String> al = new ArrayList<String>();
// adding elements of array to arrayList.
Collections.addAll(al, geeks);
System.out.println(al);
}
}
Output
[Rahul, Utkarsh, Shubham, Neelam]
2. How to Make a Collection Read-Only in Java?
// Java Program to make
// Set Interface Object Read-Only
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
// Driver Class
class GFG {
// Main Function
public static void main(String[] args)
{
// Set of Integer
Set<Integer> numbers = new HashSet<Integer>();
// Set have 1 to 10 numbers
for (int i = 1; i <= 5; i++) {
numbers.add(i);
}
// print the integers
numbers.stream().forEach(System.out::print);
// Removing element from the list
numbers.remove(5);
System.out.println("\nAfter Performing Operation");
numbers.stream().forEach(System.out::print);
System.out.println(
"\nSet is also By Default Readable and Writable");
// Now making Read-Only Set
// Using unmodifiableSet() method.
try {
numbers = Collections.unmodifiableSet(numbers);
// This line will generate an Exception
numbers.remove(4);
}
catch (UnsupportedOperationException
unsupportedOperationException) {
System.out.println(
"Exceptions is "
+ unsupportedOperationException);
}
finally {
System.out.println(numbers.contains(3));
System.out.println("Now Set is Read-Only");
}
}
}
Output
12345 After Performing Operation 1234 Set is also By Default Readable and Writable Exceptions is java.lang.UnsupportedOperationException true Now Set is Read-Only
3. Java Program to Compare Elements in a Collection.
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.
4. Java Program to Remove a Specific Element From a Collection
Input: ArrayList : [10, 20, 30, 1, 2] Index: 1 Output: ArrayList : [10, 30, 1, 2] Explanation: Removed element from index 1.
5. How to Get a Size of Collection in Java?
Input: Linked_List: [geeks, for, geeks] Output: 3 Explanation: As there are only 3 elements in the Collection So the value returned is 3.
Java Collection Exercise
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. However, programmers often find it difficult to find a platform to Practice Java Online.
Take a look at our Java Collection Exercise that contains Java Collection Practice Problems to practice and to develop your Java Programming skills. Our Java Collection Practice Exercise will cover all the topics from Basic to Advanced, from ArrayList to Priority Queue. So, let’s dive in and sharpen our Java Collection skills together!
Table of Content
- Basic Program
- ArrayList Problems
- LinkedList Problems
- HashMap Problems
- HashSet Problems
- Stack Problems
- Queue Problems
- Priority Queue Problems