How To Occupation Read Solely List, Map In Addition To Ready Inwards Coffee – Unmodifiable Collection Example

Read alone List, Map in addition to Set inwards Java
A read alone List agency a List where yous tin forcefulness out non perform modification operations similar add, remove or set. You tin forcefulness out alone read from the List past times using larn method or past times using Iterator of List, This sort of List is practiced for a for sure requirement where parameters are concluding in addition to tin forcefulness out non last changed. In Java, yous tin forcefulness out purpose Collections.unModifiableList() method  to create read alone List , Collections.unmodifiableSet() for creating read-only Set similar read alone HashSet in addition to similarly creating a read-only Map inwards Java, every bit shown inwards below example. Any modification inwards read alone List volition consequence inwards java.lang.UnSupportedOperationException in Java.  This read-only List event is based on Java v generics but likewise applicable  to other Java versions similar JDK 1.4 or JDK 1.3, simply take Generics code i.e. angle bracket which is non supported prior to Java 5. One mutual error programmer makes is that assuming fixed size List in addition to read alone List every bit same. 

As shown inwards our 3 event of converting Array to Array List , nosotros tin forcefulness out purpose Arrays.asList() method to create in addition to initialized List at same line. List implementation returned past times this method is fixed size in addition to it doesn’t allow adding or removal of chemical factor but it is non read alone because yous tin forcefulness out update objects past times calling set(index) method. How to brand a collection read alone is likewise a popular Java collection interview question, which makes this Java collection tutorial fifty-fifty to a greater extent than important.

Read alone List, Set in addition to Map Example - Java


Here is sample Java programme which demonstrate method of creating read alone List, Set in addition to Map inwards Java. You tin forcefulness out brand whatsoever List, Set or Map implementation every bit read alone past times next code example. Just recall that Collections.unModifiableList() , Collections.unModifiableSet() in addition to Collections.unModifiableMap() method inwards Java

 where yous tin forcefulness out non perform modification operations similar  How to Create Read Only List, Map in addition to Set inwards Java – UnModifiable Collection ExampleArrayList, LinkedList or Vector to read alone ArrayList , LinkedList in addition to Vector inwards Java.

package example;

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



/**
 * Java programme to create read alone List, Set in addition to Map inwards Java. You tin forcefulness out showtime create
 * a List or Set in addition to than arrive unmodifiable or read-only past times
 * using  Collections.unmodifiableList() or Collections.unmodifiableSet() method.
 *
 * @author Javin Paul
 */

public class ReadOnlyListSetMap {
 
    public static void main(String args[]) {            
       
        // creating List inwards Java
        List<String> contents = new ArrayList<String>();
     
        // initializing List inwards Java
        contents.add("Example");
        contents.add("Tutorial");
        contents.add("Program");
     
     
        // Currently This List is non read only, yous tin forcefulness out add together or take elements from List
        contents.add("Tips"); //should non last allowed if List is read only.
     
        System.err.println("normal List inwards Java : " + contents);
     
        //creating readonly List from contents
        contents = Collections.unmodifiableList(contents);
     
        //java.lang.UnsupportedOperationException -- no modification inwards read alone list
     
       //not allowed every bit it is read-only List inwards Java
       contents.add("Can I add together object into read alone List - No");

       
        contents.remove("Example"); //remove non allowed inwards read alone list
     
        //java.lang.UnSupportedOperation - List update non allowed
        contents.set(0, "Can I override or laid object inwards read-only Set - No");

     
        //Creating read alone Set inwards Java
        //similar to read-only List yous tin forcefulness out likewise create a Set which is read alone
        //i.e. improver , removal in addition to modification functioning is non permitted on list
     
     
        //Creating a Set based on contents of List      
        Set<String> readOnlySet = new HashSet<String>(contents);
     
        System.out.println("original Set inwards Java : " + readOnlySet);

        //Set is non yet read-only yous tin forcefulness out nonetheless add together elements into Set
        readOnlySet.add("Override");

        System.out.println("Set earlier making read alone : " + readOnlySet);
     
        //making Set readonly inwards Java - no add together take or laid functioning permitted
        readOnlySet = Collections.unmodifiableSet(readOnlySet);
     
        //trying to add together chemical factor inwards read alone Set - java.lang.UnSupportedOperationException
        readOnlySet.add("You tin forcefulness out non add together chemical factor inwards read Only Set");
     
        //trying to take chemical factor from read alone set
        readOnlySet.remove("Example"); //you tin forcefulness out non take elements from read alone Set
     
     
     
        // Creating read alone Map inwards Java
        // Similar to List in addition to Set yous tin forcefulness out likewise create read alone or unmodifiable Map inwards Java
        // add together , take in addition to override is non allowed on read alone Map inwards Java
     
        Map<String, String> contries = new HashMap<String, String>();      
        contries.put("India", "New Delhi");
     
        //Map is non read alone yet, yous tin forcefulness out nonetheless add together entries into
        contries.put("UK", "London");
     
        System.out.println("Map inwards Java earlier making read only: " + contries);
     
        //Making Map read alone inwards Java
        Map readOnlyMap = Collections.unmodifiableMap(contries);
       
        //you tin forcefulness out non lay a novel entry inwards read alone Map inwards Java
        readOnlyMap.put("USA", "Washington"); //java.lang.UnSupportedOperation
       
        //you tin forcefulness out non take keys from read alone Map inwards Java
        readOnlyMap.remove("UK"); //java.lang.UnSupportedOperation
     
    }
 
}


That’s all on how to create read alone List, Set in addition to Map inwards Java. Its real slow to brand whatsoever List implementation read alone past times wrapping it amongst Collections.unModifiableList(). Use same technique to convert whatsoever other Collection into read alone inwards Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt ArrayList in addition to Vector inwards Java

0 Response to "How To Occupation Read Solely List, Map In Addition To Ready Inwards Coffee – Unmodifiable Collection Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel