How To Practice Read Entirely List, Map As Well As Gear Upward Inwards Coffee – Unmodifiable Collection Example

Read exclusively List, Map as well as Set inwards Java
A read exclusively List agency a List where yous tin post away non perform modification operations similar add, remove or set. You tin post away exclusively read from the List past times using instruct method or past times using Iterator of List, This variety of List is proficient for a for sure requirement where parameters are in conclusion as well as tin post away non endure changed. In Java, yous tin post away purpose Collections.unModifiableList() method  to create read exclusively List , Collections.unmodifiableSet() for creating read-only Set similar read exclusively HashSet as well as similarly creating a read-only Map inwards Java, equally shown inwards below example. Any modification inwards read exclusively List volition upshot inwards java.lang.UnSupportedOperationException in Java.  This read-only List instance is based on Java five generics but equally good applicable  to other Java versions similar JDK 1.4 or JDK 1.3, simply take away 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 as well as read exclusively List equally same. 

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

Read exclusively List, Set as well as Map Example - Java


Here is sample Java programme which demonstrate method of creating read exclusively List, Set as well as Map inwards Java. You tin post away brand whatever List, Set or Map implementation equally read exclusively past times next code example. Just shout out back that Collections.unModifiableList() , Collections.unModifiableSet() as well as Collections.unModifiableMap() method inwards Java

 where yous tin post away non perform modification operations similar  How to Create Read Only List, Map as well as Set inwards Java – UnModifiable Collection ExampleArrayList, LinkedList or Vector to read exclusively ArrayList , LinkedList as well as 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 exclusively List, Set as well as Map inwards Java. You tin post away starting fourth dimension create
 * a List or Set as well as 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 post away add together or take away elements from List
        contents.add("Tips"); //should non endure 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 exclusively list
     
       //not allowed equally it is read-only List inwards Java
       contents.add("Can I add together object into read exclusively List - No");

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

     
        //Creating read exclusively Set inwards Java
        //similar to read-only List yous tin post away equally good create a Set which is read exclusively
        //i.e. add-on , removal as well as 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 post away notwithstanding add together elements into Set
        readOnlySet.add("Override");

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


That’s all on how to create read exclusively List, Set as well as Map inwards Java. Its really tardily to brand whatever List implementation read exclusively past times wrapping it amongst Collections.unModifiableList(). Use same technique to convert whatever other Collection into read exclusively inwards Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt ArrayList as well as Vector inwards Java

Sumber https://javarevisited.blogspot.com/

0 Response to "How To Practice Read Entirely List, Map As Well As Gear Upward Inwards Coffee – Unmodifiable Collection Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel