We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 30 May 2015

How to make collection read-only

Following example shows how to make collection read-only using Collections.unmodifiableSet(collection) method in Java.

<String> List<String> java.util.Collections.unmodifiableList(List<? extends String> list)

unmodifiableList
public static <T> List<T> unmodifiableList(List<? extends T> list) 
Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.The returned list will be serializable if the specified list is serializable. Similarly, the returned list will implement RandomAccess if the specified list does.
Parameters:
    list - the list for which an unmodifiable view is to be returned.
Returns:
    an unmodifiable view of the specified list.
 
<String> Set<String> java.util.Collections.unmodifiableSet(Set<? extends String> s)

unmodifiableSet
public static <T> Set<T> unmodifiableSet(Set<? extends T> s)
Returns an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException.The returned set will be serializable if the specified set is serializable.
Parameters:
    s - the set for which an unmodifiable view is to be returned.
Returns:
    an unmodifiable view of the specified set.

<Object, Object> Map<Object, Object> java.util.Collections.unmodifiableMap(Map<? extends Object, ? extends Object> m)

unmodifiableMap
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m) 
Returns an unmodifiable view of the specified map. This method allows modules to provide users with "read-only" access to internal maps. Query operations on the returned map "read through" to the specified map, and attempts to modify the returned map, whether direct or via its collection views, result in an UnsupportedOperationException.The returned map will be serializable if the specified map is serializable.
Parameters:
    m - the map for which an unmodifiable view is to be returned.
Returns:
    an unmodifiable view of the specified map.

Sample Program
ReadOnlyCollection.java

package com.javatutorialcorner.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ReadOnlyCollection {

	public static void main(String[] args) {
		String[] nameArray = new String[] { "Spring", "Struts", "Hibernate",
				"Webservice", "SOAP Webservice", "RESTful Webservice" };
		List<String> nameList = Arrays.asList(nameArray);
		List<String> list = new ArrayList<String>(nameList);
		list = Collections.unmodifiableList(list);
		try {
			list.set(0, "JSON");
		} catch (UnsupportedOperationException e) {
			System.out.println("Cannot add value to list");
		}

		Set<String> set = new HashSet<String>(nameList);
		set = Collections.unmodifiableSet(set);

		Map map = new HashMap();
		map = Collections.unmodifiableMap(map);

	}

}


Result
The above code will produce the following output.
Cannot add value to list

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to make collection read-only Rating: 5 Reviewed By: eHowToNow