Tuesday 15, Apr 2025
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

Related Posts:

  • How to Create Directory using Java File.mkdirs boolean java.io.File.mkdirs() Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of th… Read More
  • How to search a word inside StringFollowing program shows how to search the word inside a string using indexOf(String str) available in Java. The indexOf(String str)  method returns the position of word with in the string if found, otherwise it returns –… Read More
  • 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> li… Read More
  • How to get Character unicode point in a StringFollowing example shows how to get unicode point of a character in a String using str.codePointAt(int position) method. Sample Programpackage com.javatutorialcorner.javastring;public class StringUnicode { public static voi… Read More
  • How to search file from directory using Java String[] java.io.File.list(FilenameFilter filter) Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. The behavior of this metho… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

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