A view in Java Collections Framework is a special lightweight object which implements Map
or Collection
.
It is not a real, wholesome collection object. It does not holds extra information or operation on its own. It only acts as a particular representation of the whole or parts of another collection. As its name suggests, a view provides a different “view”, or look, into an already existing object.
Internally, a view references another collection, array, or singleton object. Hence, changing the view (if it is mutable) is equivalent to changing the referenced collection, array or object.
The term “view” probably comes from database jargon and it is closely related to Adapter pattern.
Common Views
////Map's keySet, entrySet and values
Map<String, Integer> origMap = new HashMap<>();
origMap.put("one", 1);
origMap.put("two", 2);
origMap.put("nice", -1);
Set<String> keySet = origMap.keySet();
Set<Map.Entry<String, Integer>> entrySet = origMap.entrySet();
Collection<Integer> values = origMap.values();
////Array's List representation
String[] origArr = {"one", "two", "nice"};
List<String> listOfArr = Arrays.asList(origArr);
origArr[0] = "zero";
System.out.println(listOfArr.get(0)); //"zero", they are synced
listOfArr.set(0, "what");
System.out.println(origArr[0]); //"what"
////List's subList
List<Integer> origList = new ArrayList<>();
origList.add(1);
origList.add(2);
origList.add(3);
List<Integer> subList = origList.subList(1, 2);
////SortedSet, SortedMap has similar sub-collections
//SortedSet<E> headSet(E toElement);
//SortedSet<E> subSet(E fromElement, E toElement);
//SortedSet<E> tailSet(E fromElement);
//SortedMap<K,V> headMap(K toKey);
//SortedMap<K,V> subMap(K fromKey, K toKey);
//SortedMap<K,V> tailMap(K fromKey);
Unmodifiable Views
List<String> zeroList = Collections.emptyList();
Set<String> zeroSet = Collections.emptySet();
Map<String, Integer> zeroMap = Collections.emptyMap();
List<String> oneList = Collections.singletonList("element");
Set<String> oneSet = Collections.singleton("element");
Map<String, Integer> oneMap = Collections.singletonMap("one", 1);
List<String> NineElemsList = Collections.nCopies(9, "element");
These views are unmodifiable and will error if you try add or remove.
They are easier and faster to create as they do not have the overhead of creating a traditional collection.
Further, java creates factory methods to create unmodifiable collection (these are views, too):
//List<T> unmodifiableList(List<? extends T> list);
//Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m);
//Set<T> unmodifiableSet(Set<? extends T> s);
//SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m);
//SortedSet<T> unmodifiableSortedSet(SortedSet<T> s);
//For example
List<Integer> unmodifiableList = Collections.unmodifiableList(origList);
Other than those, to create unmodifiable lists you can also use,
List<String> unmodifiableList2 = Arrays.asList("1", "2", "3");
As of Java9, to create unmodifiable maps with multiple entries,
Map<String, Integer> unmodifiableMap2 = Map.ofEntries( entry("a", 1), entry("b", 2) );
Map<String, Integer> unmodifiableMap3 = Map.of("a", 1, "b", 2);
Unmodifiable list and immutable list are pretty much two terms that could be used interchangeably.
Unmodifiable map and immutable map are not same. You cannot add or remove entries from unmodifiable map (UnsupportedOperationException
). You can still update key values in the case of unmodifiable map. An unmodifiable Map containing immutable objects is called immutable Map. In immutable map nothing can be changed.
Why Bother?
View exists for the primary reason of efficiency. No copying around data. No extra codes. Minimized overhead.
- Post link: https://reimirno.github.io/2022/04/01/Views-in-Java-Collection-Framework/
- Copyright Notice: All articles in this blog are licensed under unless otherwise stated.
GitHub Discussions