A view of another object doesn't contain its own data at all. All of its operations are implemented in terms of operations on the other object.
For example, the keySet()
view of a Map
might have an implementation that looks something like this:
class KeySet implements Set<K> {
private final Map<K, V> map;
public boolean contains(Object o) {
return map.containsKey(o);
}
...
}
In particular, whenever you modify the backing object of your view -- here, the Map
backs the keySet()
-- the view reflects the same changes. For example, if you call map.remove(key)
, then keySet.contains(key)
will return false
without you having to do anything else.
Alternately, Arrays.asList(array)
provides a List
view of that array.
String[] strings = {"a", "b", "c"};
List<String> list = Arrays.asList(strings);
System.out.println(list.get(0)); // "a"
strings[0] = "d";
System.out.println(list.get(0)); // "d"
list.set(0, "e");
System.out.println(strings[0]); // "e"
A view is just another way of looking at the data in the original backing object -- Arrays.asList
lets you use the List
API to access a normal array; Map.keySet()
lets you access the keys of a Map
as if it were a perfectly ordinary Set
-- all without copying the data or creating another data structure.
Generally, the advantage of using a view instead of making a copy is the efficiency. For example, if you have an array and you need to get it to a method that takes a List
, you're not creating a new ArrayList
and a whole copy of the data -- the Arrays.asList
view takes only constant extra memory, and just implements all the List
methods by accessing the original array.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…