集合视图是什么? [英] What is a view of a collection?
问题描述
在使用番石榴时,我已经读过几次 view 。 a>收集并阅读其文档。
I've been reading the term view a few times when using Guava collections and reading its documentation.
我一直在寻找一种解释,说明在这种情况下视图是什么以及它是否在Guava之外使用。在此处,它经常被使用。此类型来自Guava的a>名称中有 view 。
I've looked for an explanation of what a view is in this context and whether it's a term used outside of Guava. It's quite often used here. This type from Guava has view in its name.
我的猜测是,一个集合的视图是另一个具有相同数据但结构化的集合不同地例如,当我将条目从 java.util.HashSet
添加到 java.util.LinkedHashSet
时,后者是前者的看法。
My guess is that a view of a collection is another collection with the same data but structured differently; for instance when I add the entries from a java.util.HashSet
to a java.util.LinkedHashSet
the latter would be a view of the former. Is that correct?
如果有的话,有人可以用指向 view 的公认定义的链接吸引我吗?
Can somebody hook me up with a link to an accepted definition of view, if there is one?
谢谢。
推荐答案
另一个对象的视图根本不包含自己的数据。它的所有操作都是根据对另一个对象的操作实现的。
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.
例如, keySet()
地图
的视图可能具有如下所示的实现:
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);
}
...
}
特别是,只要您修改视图的后备对象-在这里, Map
后退 keySet()
-视图反映了相同的更改。例如,如果调用 map.remove(key)
,则 keySet.contains(key)
将返回 false
,而无需执行其他任何操作。
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.
或者, Arrays.asList(array)
提供该数组的列表
视图。
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"
视图只是查看原始支持对象中数据的另一种方式- Arrays.asList
允许您使用 List
API访问普通数组; Map.keySet()
允许您访问 Map
的键,就好像它是完全普通的 Set
-全部无需复制数据或创建其他数据结构。
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.
通常,使用视图而不是复制是效率。例如,如果您有一个数组,并且需要将其添加到采用 List
的方法中,则无需创建新的 ArrayList
和数据的完整副本- Arrays.asList
视图仅占用恒定的额外内存,并且仅实现所有通过访问原始数组来列出
方法。
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.
这篇关于集合视图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!