Read Only Collections
-
Upload
pankaj-jagadale -
Category
Documents
-
view
222 -
download
0
Transcript of Read Only Collections
-
7/31/2019 Read Only Collections
1/4
Read Only Collections
September 25th, 2009
I do build crazy buildings using my collection of Lego blocks. My 11 months old kid Bencuriously stares at me build it. He always wishes to get hold of it. After I complete the building
when I give that to his hand, you know what the first thing he does.
Modify the building blocks. Though I wish them to be intact forever Lego buildings are built to
be modified.
But this is not the case in programming. You create a java collection and store objects in it. Then
there are scenarios where you want them not be modified. Obsessed with file system terminology
Java guys have named it as read only collections.
By default some of the languages like dot net provide read only collections. But in Java there are
no such things. This is not a special type of collection it is an additional facility provided to
change the usual collections as read only.
Methods by Collections class
The Collections class provides six factory methods, one for each of Collection, List, Map, Set,
SortedMap, and SortedSet.
Collection unmodifiableCollection(Collection collection) List unmodifiableList(List list)
Map unmodifiableMap(Map map) Set unmodifiableSet(Set set)
SortedMap unmodifiableSortedMap(SortedMap map) SortedSet unmodifiableSortedSet(SortedSet set)
-
7/31/2019 Read Only Collections
2/4
You should set the collection with required values then pass it as value to the Collections
respective method. Most important thing here is, just passing and setting it as unModifiableX is
not enough. These methods will return you collection as read only. You need to overwrite your
old collection with this new read only collection. If you dont do that, using the reference of theold collection the values can be modified. Cool right!
The returned set will be serializable if the specified set is serializable. If you attempt to modify a
read-only collection it will throw an UnsupportedOperationException.
Example source code for java read only collections
01.importjava.util.ArrayList;02.importjava.util.Arrays;03.importjava.util.Collections;04.importjava.util.HashMap;05.importjava.util.HashSet;06.importjava.util.List;07.importjava.util.Map;08.importjava.util.Set;09.
10.// example program to demonstrate the read-only collection in java
11.publicclassReadOnlyCollections {12.
13.publicstaticvoidmain(String args[]) {14.
15.// creating a list
16.List godList = Arrays
17..asList(newString[] { "Donald", "Dennis", "Ken"});18.
19.// making a read-only list20.List list = newArrayList(godList);21.list = Collections.unmodifiableList(list);
22.
23.// checking the reference in a read-only set
24.Set set = newHashSet(godList);25.Collections.unmodifiableSet(set);
26.
27.// the following statement allows to modify the above set as the
28.// reference is pointing to the original collection therefore it is not
29.// read-only
30.set.add("Alan");
31.32.// making a read-only map and try to modify it
33.Map godMap = newHashMap();34.godMap.put("TAOCP", "Donald");
35.godMap.put("C", "Dennis");
36.
37.godMap = Collections.unmodifiableMap(godMap);
38.
39.try{
-
7/31/2019 Read Only Collections
3/4
40.// modifying the read-only map to check what happens
41.godMap.put("Unix", "Ken");
42.} catch(UnsupportedOperationException e) {43.System.out.println("You cannot modify a read only collection!");
44.}
45.}
46.}Difference between Vector and ArrayList in
java?
June 4th, 2008
java.util.Vector came along with the first version of java development kit (JDK).
java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As
per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector
also became a part of java collection framework.
All the methods of Vector is synchronized. But, the methods of ArrayList is notsynchronized. All the new implementations of java collection framework is not synchronized.
Vector and ArrayList both uses Array internally as data structure. They are dynamically
resizable. Difference is in the way they are internally resized. By default, Vector doubles the size
of its array when its size is increased. But, ArrayList increases by half of its size when its size is
increased.
Therefore as per Java API the only main difference is, Vectors methods are synchronized andArrayLists methods are not synchronized.
Vector or ArrayList? Which is better to use in java?
In general, executing a synchronized method results in costlier performance than a
unsynchronized method. Keeping the difference in mind, using Vector will incur a performance
hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to
be used.
Is there an alternate available in java for Vector?
ArrayList can be synchronized using the java collections framework utility class and then
ArrayList itself can be used in place of Vector.
When there is no need for synchronized operation and you still look for better performance
Array can be used instead of ArrayList. But the development is tedious, since it doesnt provideuser friendly methods.
When you use Vector or ArrayList, always initialize to the largest capacity that the java program
will need. Since incrementing the size is a costlier operation.
-
7/31/2019 Read Only Collections
4/4