java - 在 JDK 1.5 中使用 Collections.newSetFromMap 的替代方案?

标签 java collections java-5

我想在不支持它的 JDK 1.5 中使用这种“Collections.newSetFromMap()”方法。 Java 5 也不支持 ConcurrentHashSet 类。 必须在 JDK 1.5 中编译以下行。我该怎么做?

protected Set<String> knownLCWords = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());

请指导我。

最佳答案

下面是来自 Java Collections source 的那个方法的实现。 .本质上,该方法返回包含 MapAbstractSet 子类,以及一个 transient Set

/**
 * Returns a set backed by the specified map.  The resulting set displays
 * the same ordering, concurrency, and performance characteristics as the
 * backing map.  In essence, this factory method provides a {@link Set}
 * implementation corresponding to any {@link Map} implementation.  There
 * is no need to use this method on a {@link Map} implementation that
 * already has a corresponding {@link Set} implementation (such as {@link
 * HashMap} or {@link TreeMap}).
 *
 * <p>Each method invocation on the set returned by this method results in
 * exactly one method invocation on the backing map or its <tt>keySet</tt>
 * view, with one exception.  The <tt>addAll</tt> method is implemented
 * as a sequence of <tt>put</tt> invocations on the backing map.
 *
 * <p>The specified map must be empty at the time this method is invoked,
 * and should not be accessed directly after this method returns.  These
 * conditions are ensured if the map is created empty, passed directly
 * to this method, and no reference to the map is retained, as illustrated
 * in the following code fragment:
 * <pre>
 *    Set&lt;Object&gt; weakHashSet = Collections.newSetFromMap(
 *        new WeakHashMap&lt;Object, Boolean&gt;());
 * </pre>
 *
 * @param map the backing map
 * @return the set backed by the map
 * @throws IllegalArgumentException if <tt>map</tt> is not empty
 * @since 1.6
 */
public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
    return new SetFromMap<E>(map);
}

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();
    }
}

关于java - 在 JDK 1.5 中使用 Collections.newSetFromMap 的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528440/

相关文章:

java - 计算器错误

java - 使用来自 Hibernate 实体的现有数据填充 envers 修订表

java - Maven 提示 Joda-Time 即使我安装了它

Java 将 Int 数组转换为 SortedSet

console - 我们什么时候应该使用控制台类?

Frame.add(Object) 处的 java.lang.NullPointerException

java - Collections.syncronizedList 实际上是一个 vector 吗?

java - 使用集合对象还是Java传输对象的性能比较(以堆大小而言)

java - 将应用程序从 Java 1.6 降级到 Java 1.5

java - jdk安装文件夹可以复制吗