10:49 am - Sunday April 30, 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

 
</div>
<div class="toolbar"></div>
<div class="toolbar">package com.example;import java.util.*;

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

// create a array list
List&lt;Character&gt; list = new ArrayList&lt;Character&gt;();
// populate the list
list.add('a');
list.add('b');

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

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

}

</div>
<div class="toolbar"></div>
<div class="toolbar"></div>
<div class="toolbar"></div>
<div class="toolbar">

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.