Skip to content
Related Articles
Open in App
Not now

Related Articles

Iterate Over Unmodifiable Collection in Java

Improve Article
Save Article
  • Last Updated : 04 Jan, 2021
Improve Article
Save Article

The Collection is a framework that provides an architecture to store and manipulate the group of objects. In Java Collections, we perform operations like searching, sorting, Iterating over the given set of elements.

Modifiable: Modifiable means we can perform operations like adding, deleting, updating elements in collection data structures.

Example: Java List provides modification methods which include, add() method to add the elements in a list, replaceall() method to change the elements in a list.

Unmodifiable: Unmodifiable means we cannot perform adding, deleting, updating elements in collection data structures.

Example: We can initialize the modifiable class before any data structure so that we can prevent it from updating of any elements in the given data structure. We can create an unmodifiable view of a collection using Collections.unmodifiableCollection(collection)

Declaration

public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)

Syntax:

Collections.unmodifiableCollection(collection)

Parameters: This method takes the collection as a parameter for which an unmodifiable view is to be returned.

Return Value: This method returns an unmodifiable view of the specified collection.

Example 1:

Java




// Java program to Iterate Over Unmodifiable Collection
  
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
  
public class GFG {
  
    public static void main(String args[])
    {
        // create a list
        List<String> list = new ArrayList<String>();
  
        // add elements
        list.add("welcome");
        list.add("to");
        list.add("geeks for geeks");
        list.add("This");
        list.add("is");
        list.add("Unmodifiable Collection");
  
        System.out.println("Element are added to the list: "
                           + list.get(2));
  
        // create a immutable view of the list
        Collection<String> immutableCol
            = Collections.unmodifiableCollection(list);
  
        // iterator on the immutable list
        Iterator<String> iterator = immutableCol.iterator();
  
        // print the immutable list
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Output

Element are added to the list: geeks for geeks
welcome
to
geeks for geeks
This
is
Unmodifiable Collection

Example 2:

Java




// Java program to Iterate Over Unmodifiable Collection 
  
import java.util.*;
  
public class GFG {
    
    public static void main(String args[])
    {
          // create a vector
        Vector<String> v = new Vector<String>();
        
          // add elements
        v.add("welcome");
        v.add("to");
        v.add("geeks for geeks");
        
          // create a immutable vector
        Collection<String> immutableCol
            = Collections.unmodifiableCollection(v);
        
          // iterate and print elements
        Iterator<String> iterator = immutableCol.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Output

welcome
to
geeks for geeks

Example 3: For UnsupportedOperationException

Java




// Java program to demonstrate
// unmodifiableCollection() method
// for UnsupportedOperationException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of ArrayList<Character>
            ArrayList<String> list = new ArrayList<>();
  
            // populate the list
            list.add("Geeks");
            list.add("for");
  
            // printing the list
            System.out.println("Initial list: " + list);
  
            // getting unmodifiable list
            // using unmodifiableCollection() method
            Collection<String> immutablelist
                = Collections.unmodifiableCollection(list);
  
            // Adding element to new Collection
            System.out.println(
                "\nTrying to modify"
                + " the unmodifiableCollection");
  
            immutablelist.add("Geeks");
        }
  
        catch (UnsupportedOperationException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output

Initial list: [Geeks, for]
Trying to modify the unmodifiableCollection
Exception thrown : java.lang.UnsupportedOperationException

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!