10:27 pm - Monday March 27, 2017

The unmodifiableCollection() method is used to return an unmodifiable view of the specified collection using unmodifiableCollection(Collection c)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 

        The unmodifiableCollection() method is used to return an unmodifiable view of the specified collection

Example Program

import java.util.*;

public class UnmodifiableCltion {
	public static void main(String[] args) {

		// create a array list
		List<Character> list = new ArrayList<Character>();
		// populate the list
		list.add('a');
		list.add('b');

		System.out.println("Initial list: " + list);
		// make the list unmodifiable
		Collection<Character> immutablelist = Collections
				.unmodifiableCollection(list);

		// try to modify the list
		immutablelist.add('c');
	}

}
[/java]

Output: Initial list: [a, b]

Exception in thread “main” java.lang.UnsupportedOperationException

at java.util.Collections$UnmodifiableCollection.add(Unknown Source)

at SecondTask.UnmodifiableCltion.main(UnmodifiableCltion.java:22)

Explanation:

The unmodifiableCollection() method is used to return an unmodifiable view of the specified collection.And an attempt to modify the collection will result in an UnsupportedOperationException.


Parameters:  c–This is the collection for which an unmodifiable view is to be returned.


Returns: The method call returns an unmodifiable view of the specified collection.

Throws: NA

BACK

Filed in: Z-A All Java Codes

Comments are closed.