1:41 am - Wednesday February 22, 2017

The unmodifiableList() method is used to returns an unmodifiable view of the specified list unmodifiableList(List list)

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 unmodifiableList() method is used to returns an unmodifiable view of the specified list.

Example Program


import java.util.*;

public class Unmodifiablelst {
	public static void main(String[] args) {
		// creating arraylist
		List list = new ArrayList();
		// populate the list
		list.add('a');
		list.add('c');
		// Initial list
		System.out.println("Initial list: " + list);

		// make the list unmodifiable
		List immutable = Collections.unmodifiableList(list);

		// try to modify list
		immutable.add('d');
	}

}

Output: Initial list: [a, c]

Exception in thread “main” java.lang.UnsupportedOperationException

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

at SecondTask.Unmodifiablelst.main(Unmodifiablelst.java:19)

Explanation:

The unmodifiableList() method is used to returns an unmodifiable view of the specified list.

Parameter: list–This is the list for which an unmodifiable view is to be returned.

Return:  The method call returns an unmodifiable view of the specified list.

Throws: NA

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.