Read solely Collection inwards Java
You tin exercise read-only Collection past times using Collections.unmodifiableCollection() utility method. it returns a unmodifiable or read-only sentiment of Collection inwards which you lot tin non perform whatever performance which volition modify the collection similar add() , remove() together with set() either straight or spell iterating using Iterator or ListIterator. It volition throw UnsupportedOperationException whenever you lot essay to modify the List. One of the mutual misconception around read solely ArrayList is that, you lot tin exercise read-only ArrayList past times using Arrays.asList(String{[]), which is obviously non truthful every bit this method solely render a fixed size list on which add() together with remove() are non allowed past times set() method is nevertheless allowed which tin modify the contents of ArrayList. Collections class every bit good supply a dissimilar method to brand List together with Set read-only. In this Java tutorial, nosotros volition acquire How to brand whatever collection read only together with How to exercise fixed size List every bit well.
Making ArrayList Read Only - Java
here is code representative of creating together with making existing ArrayList read only. This representative uses Arrays.asList() method to exercise a fixed length ArrayList which is initialized during creation on same job together with than subsequently wrapped it into unmodifiable collection to expire far read only.
Java Program to exercise unmodifiable ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Java programme to create read solely Collection inwards Java.
* obviously you lot tin brand any
* Collection read solely past times using
* Collections.unmodifiableCollection(), at that spot are separate
* method for List together with Set every bit well.
*
* @author Javin Paul
*/
public class ReadOnlyCollection {
public static void main(String args[]) {
//creating read solely Collection inwards Java
Collection readOnlyCollection = Collections.unmodifiableCollection(new ArrayList<String>());
readOnlyCollection.add("Sydney Sheldon"); //raises UnSupportedOperation exception
//making existing ArrayList readonly inwards Java
ArrayList readableList = new ArrayList();
readableList.add("Jeffrey Archer");
readableList.add("Khalid Hussain");
List unmodifiableList = Collections.unmodifiableList(readableList);
//add volition throw Exception because List is readonly
unmodifiableList.add("R.K. Narayan");
//remove is non allowed inwards unmodifiable list
unmodifiableList.remove(0);
//set is non allowed inwards unmodifiable List
unmodifiableList.set(0, "Anurag Kashyap");
//creating Fixed Length List from Array inwards Java
List fixedLengthList = Arrays.asList("Mark" , "Twen");
// readOnlyList.add("J.K. Rowling"); //raises Exception
fixedLengthList.set(0, "J.K. Rowling"); //allowed that's why non read solely list
System.out.println(fixedLengthList.get(0));
}
}
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Java programme to create read solely Collection inwards Java.
* obviously you lot tin brand any
* Collection read solely past times using
* Collections.unmodifiableCollection(), at that spot are separate
* method for List together with Set every bit well.
*
* @author Javin Paul
*/
public class ReadOnlyCollection {
public static void main(String args[]) {
//creating read solely Collection inwards Java
Collection readOnlyCollection = Collections.unmodifiableCollection(new ArrayList<String>());
readOnlyCollection.add("Sydney Sheldon"); //raises UnSupportedOperation exception
//making existing ArrayList readonly inwards Java
ArrayList readableList = new ArrayList();
readableList.add("Jeffrey Archer");
readableList.add("Khalid Hussain");
List unmodifiableList = Collections.unmodifiableList(readableList);
//add volition throw Exception because List is readonly
unmodifiableList.add("R.K. Narayan");
//remove is non allowed inwards unmodifiable list
unmodifiableList.remove(0);
//set is non allowed inwards unmodifiable List
unmodifiableList.set(0, "Anurag Kashyap");
//creating Fixed Length List from Array inwards Java
List fixedLengthList = Arrays.asList("Mark" , "Twen");
// readOnlyList.add("J.K. Rowling"); //raises Exception
fixedLengthList.set(0, "J.K. Rowling"); //allowed that's why non read solely list
System.out.println(fixedLengthList.get(0));
}
}
If you lot un-comment lines which supposed to heighten Exception thus you lot volition acquire Exception similar this:
Exception inwards thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1018)
at test.CollectionTest.main(CollectionTest.java:24)
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1018)
at test.CollectionTest.main(CollectionTest.java:24)
If you lot comment all the lines which exercise modification on read-only listing thus you lot tin exam the concluding job which changes the value of an chemical constituent inwards fixed length ArrayList.
That’s all on How to exercise read solely Collection inwards Java. Read solely collection is every bit good called unmodifiable collection together with does non allow whatever modification performance similar add() , remove() or set() operation. Remember read-only collection is dissimilar than fixed length collection which does non allow add together or take method but allow to update the value of an existing chemical constituent using the laid method.
Further Learning
Java In-Depth: Become a Complete Java Engineer
answer)
No comments:
Post a Comment