简体   繁体   English

如何制作一张由地图支持的套装?

[英]How to make a set backed by a map?

There is a method in the Collections class. Collections类中有一个方法。

Set<E> Collections.newSetFromMap(<backing map>)

What does it mean by the backing map and the set backed by a map ? 支持地图地图 支持的集合意味着什么?

Perhaps it would be illuminating to look at the implementation: 看看实施可能会很有启发性:

private static class SetFromMap<E> extends AbstractSet<E>
    implements Set<E>, Serializable
{
    private final Map<E, Boolean> m;  // The backing map
    private transient Set<E> s;       // Its keySet

    SetFromMap(Map<E, Boolean> map) {
        if (!map.isEmpty())
            throw new IllegalArgumentException("Map is non-empty");
        m = map;
        s = map.keySet();
    }

    public void clear()               {        m.clear(); }
    public int size()                 { return m.size(); }
    public boolean isEmpty()          { return m.isEmpty(); }
    public boolean contains(Object o) { return m.containsKey(o); }
    public boolean remove(Object o)   { return m.remove(o) != null; }
    public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
    public Iterator<E> iterator()     { return s.iterator(); }
    public Object[] toArray()         { return s.toArray(); }
    public <T> T[] toArray(T[] a)     { return s.toArray(a); }
    public String toString()          { return s.toString(); }
    public int hashCode()             { return s.hashCode(); }
    public boolean equals(Object o)   { return o == this || s.equals(o); }
    public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
    public boolean removeAll(Collection<?> c)   {return s.removeAll(c);}
    public boolean retainAll(Collection<?> c)   {return s.retainAll(c);}
    // addAll is the only inherited implementation

    private static final long serialVersionUID = 2454657854757543876L;

    private void readObject(java.io.ObjectInputStream stream)
        throws IOException, ClassNotFoundException
    {
        stream.defaultReadObject();
        s = m.keySet();
    }
}

Edit - added explanation: 编辑 - 添加说明:

The map that you provide is used as the m field in this object. 您提供的地图用作此对象中的m字段。

When you add an element e to the set, it adds an entry e -> true to the map. 当您向集合中添加元素e ,它会向地图添加一个条目e -> true

public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }

So this class turns your Map into an object that behaves like a Set by simply ignoring the values that things are mapped to, and just using the keys. 因此,通过简单地忽略事物映射到的值,并使用键,此类将您的Map转换为行为类似于Set的对象。

I just made an example code for you 我刚给你做了一个示例代码

HashMap<String, Boolean> map = new HashMap<String, Boolean>();

Set<String> set = Collections.newSetFromMap(map);

System.out.println(set);

for (int i = 0; i < 10; i++)
    map.put("" + i, i % 2 == 0);

System.out.println(map);

System.out.println(set);

and the output 和输出

[]
{3=false, 2=true, 1=false, 0=true, 7=false, 6=true, 5=false, 4=true, 9=false, 8=true}
[3, 2, 1, 0, 7, 6, 5, 4, 9, 8]

简而言之, Collections.newSetFromMap使用提供的Map<E>实现来存储Set<E>元素。

The Set internally uses Map to store the values. Set内部使用Map来存储值。 Here the backing map refers to the set map which is internally used by the set. 这里支持映射引用集合内部使用的集合映射。 For more information. 欲获得更多信息。 http://www.jusfortechies.com/java/core-java/inside-set.php http://www.jusfortechies.com/java/core-java/inside-set.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM