How To Utilisation Read Alone List, Map Too Gear Upward Inwards Coffee – Unmodifiable Collection Example

Read exclusively List, Map as well as Set inward Java
A read exclusively List agency a List where you lot tin non perform modification operations similar add, remove or set. You tin exclusively read from the List past times using larn method or past times using Iterator of List, This form of List is practiced for a sure enough requirement where parameters are finally as well as tin non hold upward changed. In Java, you lot tin role 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 inward Java, equally shown inward below example. Any modification inward read exclusively List volition number inward java.lang.UnSupportedOperationException in Java.  This read-only List instance is based on Java five generics but too applicable  to other Java versions similar JDK 1.4 or JDK 1.3, only take away 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 as well as read exclusively List equally same. 

As shown inward our 3 instance of converting Array to Array List , nosotros tin role 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 you lot tin update objects past times calling set(index) method. How to brand a collection read exclusively is too 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 plan which demonstrate method of creating read exclusively List, Set as well as Map inward Java. You tin brand whatsoever List, Set or Map implementation equally read exclusively past times next code example. Just recollect that Collections.unModifiableList() , Collections.unModifiableSet() as well as Collections.unModifiableMap() method inward Java

 where you lot tin non perform modification operations similar  How to Create Read Only List, Map as well as Set inward Java – UnModifiable Collection ExampleArrayList, LinkedList or Vector to read exclusively ArrayList , LinkedList as well as Vector inward 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 plan to create read exclusively List, Set as well as Map inward Java. You tin offset 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 inward Java
        List<String> contents = new ArrayList<String>();
     
        // initializing List inward Java
        contents.add("Example");
        contents.add("Tutorial");
        contents.add("Program");
     
     
        // Currently This List is non read only, you lot tin add together or take away elements from List
        contents.add("Tips"); //should non hold upward allowed if List is read only.
     
        System.err.println("normal List inward Java : " + contents);
     
        //creating readonly List from contents
        contents = Collections.unmodifiableList(contents);
     
        //java.lang.UnsupportedOperationException -- no modification inward read exclusively list
     
       //not allowed equally it is read-only List inward Java
       contents.add("Can I add together object into read exclusively List - No");

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

     
        //Creating read exclusively Set inward Java
        //similar to read-only List you lot tin too 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 inward Java : " + readOnlySet);

        //Set is non yet read-only you lot tin all the same add together elements into Set
        readOnlySet.add("Override");

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


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

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

Komentar

Postingan populer dari blog ini

2 Ways To Banking Concern Tally If A String Is Rotation Of Other Inward Java?

How To Convert String To Integer To String Inward Coffee Amongst Example

How To Induce Chrome, Firefox Blurry, Over Bright, Fading Afterwards Windows Ten Update