How To Practice Read Solely List, Map In Addition To Laid Upward Inwards Coffee – Unmodifiable Collection Example

Read solely List, Map in addition to Set inwards Java
A read solely List way a List where y'all tin sack non perform modification operations similar add, remove or set. You tin sack solely read from the List past times using larn method or past times using Iterator of List, This sort of List is adept for a certainly requirement where parameters are concluding in addition to tin sack non hold out changed. In Java, y'all tin sack purpose Collections.unModifiableList() method  to create read solely List , Collections.unmodifiableSet() for creating read-only Set similar read solely HashSet in addition to similarly creating a read-only Map inwards Java, equally shown inwards below example. Any modification inwards read solely List volition resultant inwards java.lang.UnSupportedOperationException in Java.  This read-only List event is based on Java v generics but too applicable  to other Java versions similar JDK 1.4 or JDK 1.3, simply withdraw Generics code i.e. angle bracket which is non supported prior to Java 5. One mutual fault programmer makes is that assuming fixed size List in addition to read solely List equally same. 

As shown inwards our 3 event of converting Array to Array List , nosotros tin sack 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 component but it is non read solely because y'all tin sack update objects past times calling set(index) method. How to brand a collection read solely is too a popular Java collection interview question, which makes this Java collection tutorial fifty-fifty to a greater extent than important.

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


Here is sample Java programme which demonstrate method of creating read solely List, Set in addition to Map inwards Java. You tin sack brand whatever List, Set or Map implementation equally read solely past times next code example. Just yell upward that Collections.unModifiableList() , Collections.unModifiableSet() in addition to Collections.unModifiableMap() method inwards Java

 where y'all tin sack 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 solely 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 solely List, Set in addition to Map inwards Java. You tin sack starting fourth dimension create
 * a List or Set in addition to than larn inwards 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, y'all tin sack add together or withdraw elements from List
        contents.add("Tips"); //should non hold out 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 solely list
     
       //not allowed equally it is read-only List inwards Java
       contents.add("Can I add together object into read solely List - No");

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

     
        //Creating read solely Set inwards Java
        //similar to read-only List y'all tin sack too create a Set which is read solely
        //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 y'all tin sack withal add together elements into Set
        readOnlySet.add("Override");

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


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

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

Belum ada Komentar untuk "How To Practice Read Solely List, Map In Addition To Laid Upward Inwards Coffee – Unmodifiable Collection Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel