Collections

Get unmodifiable Collection

This is an example of how to get an unmodifiable Collection. The same API applies to any type of Collection implementation classes e.g. HashSet, TreeSet, LinkedHashSet, LinkedList etc. Nevertheless Java util API provides separate methods for getting immutable Collection views based on the following Collection implementation classes:
– static Collection unmodifiableCollection(Collection)
– static List unmodifiableList(List)
– static Set unmodifiableSet(Set)
– static SortedSet unmodifiableSortedSet(SortedSet)
– static Map unmodifiableMap(Map)
– static SortedMap unmodifiableSortedMap(SortedMap).
It is highly suggested to use one of the above depending on your source Collection implementation class. In the example we are using an ArrayList as the Collection from which to get the unmodifiable Collection. Getting an unmodifiable Collection from an ArrayList implies that you should:

  • Create a new ArrayList.
  • Populate the list with elements, with the add(E e) API method of the ArrayList.
  • Create a new unmodifiable Collection, using the unmodifiableCollection(Collection c) API method of the Collections. It returns an unmodifiable view of the specified collection, and it allows modules to provide users with “read-only” access to the internal collection.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
public class UnmodifiableCollection {
 
  public static void main(String args[]) {

    /*

Please note that the same API applies to any type of 

Collection implementation classes e.g. 

HashSet, TreeSet, LinkedHashSet, LinkedList etc

Nevertheless Java util API provides separate methods for getting 

immutable Collection views based on the following Collection 

implementation classes :

- static Collection unmodifiableCollection(Collection)

- static List unmodifiableList(List)

- static Set unmodifiableSet(Set)

- static SortedSet unmodifiableSortedSet(SortedSet)

- static Map unmodifiableMap(Map)

- static SortedMap unmodifiableSortedMap(SortedMap)

It is highly suggested to use one of the above depending on your 

source Collection implementation class
     */

    // Create an ArrayList and populate it with elements
    List list = new ArrayList();
    list.add("element_1");
    list.add("element_2");
    list.add("element_3");
 
    // static unmodifiableCollection(collection) method returns an immutable Collection from the provided ArrayList
    Collection unmodifiableCollection = Collections.unmodifiableCollection(list);
 
    System.out.println("unmodifiableCollection contains : " + unmodifiableCollection);
  }
}

Output:

unmodifiableCollection contains : [element_1, element_2, element_3]

 
This was an example of how to get an unmodifiable Collection in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button