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
// 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
// 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

Similar Reads

What is Collection in Java?

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....

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....

ArrayList Problems

ArrayList is a Collection which is the implementation of Dynamic Array Data Structure....

LinkedList Problems

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....

HashMap Problems

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....

HashSet Problems

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!...

Stack Problems

Stack is part of Collection in Java, it is a Interface which is based on Stack Data Structure. It follows the principle of LIFO that is Last in First Out....

Queue Problems

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....

Priority Queue Problems

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....

Conclusion

You’ve completed these Java collection exercises.  Now you can handle ArrayLists, HashSets, Priority Queues, and more with confidence.  Ready to push your skills further?  Revisit these exercises, experiment with your own ideas, and solidify your Java collection mastery!...