【问题标题】:How to use addAll and retainAll methods in HashSet如何在HashSet中使用addAll和retainAll方法
【发布时间】:2017-04-16 16:47:43
【问题描述】:

当我尝试编译时,我的 makeUnion 方法收到一条错误消息,我猜我也会为 makeIntersection 收到一条错误消息。我不确定这是为什么,或者如果我想向新集合添加集合接口,如何实现 makeUnion。有人可以向我解释我做错了什么吗?

public class Set<T> implements SetInterface<T>
{
private HashSet<T> set;

/**
 * Constructs a new empty set.
 */
public Set () {
    set = new HashSet <>();
}

/**
 * Constructs a new set containing the elements in the specified collection. 
 * Default load factor of 0.75 and initial capacity of 50.
 * 
 * @param c- the collection whose elements are to be place into this set
 */
public Set(Collection <? extends T> c) {
    set = new HashSet<>(Math.max((int) (c.size()/.75f) + 1, 50));
    set.addAll(c);
}

/**
 * Constructs a new empty set. Default load factor of 0.75.
 * 
 * @param initialCapacity- the initial capacity of the hash table
 */
public Set(int initialCapacity) {
     set = new HashSet <>(initialCapacity);
}

/**
 * Constructs a new empty set. 
 * Hashmap has specified initial capacity and specified load factor.
 * 
 * @param initialCapacity- the initial capacity of the hash table
 *        loadFactor- the load factor of the hash map
 */
public Set(int initialCapacity, float loadFactor) {
     set = new HashSet <>(initialCapacity, loadFactor);
}

/**
 * Add an item of type T to the interface  Duplicate items will not be
 * added to the set.
 * 
 * @param  itemToAdd - what to add.
 */
public void add(T itemToAdd) {
    set.add(itemToAdd);
}

/**
 * Removes an item from the set ( if the item is in the set)  If the item is not
 * in the set this operation does nothing
 * 
 * @param  item to remove. 
 */
public void remove( T itemToDelete) {
    set.remove(itemToDelete);
}

/**
 * Return if the SetInterface contains an item
 * 
 * @param itemToCheck.  The item you are looking for
 * @return  true if found.  False if not found.
 */
public boolean contains( T itemToCheck) {
    return set.contains(itemToCheck);
}

/**
 * Make a union of two sets.  We add all items in either set to a new set and
 * return the new set.
 * 
 * @param the 'other' set to add to our set.
 * @return  A new set which is the union of the two sets. 
 */
public Set<T> makeUnion( SetInterface<T> otherSet) {
    return set.addAll(otherSet);
}

/**
 * Make an intersection  of two sets.  We add create a new set which only has
 * items in it that are contained in both sets.
 * 
 * @param the 'other' set to intersect with 
 * @return  A new set which is the intersection  of the two sets. 
 */
public Set<T> makeIntersection( SetInterface<T> otherSet) {
    return set.retainAll(otherSet);
}

/** 
 * Return an iterator for the set.  This is used to walk thought all elements
 * in the set
 * 
 * @return  The iterator
 */
public Iterator<T> getIterator() {
    return set.iterator();
}

/**
 * Tell the caller how many elements are in the set
 * 
 * @return int with the number of elements
 */
public int size() {
    return set.size();
}

}

【问题讨论】:

  • 您是否阅读了 javadoc 并查看了 HashSet.addAll() 的返回值是什么?你应该这样做,因为它显然不是你认为的那样。因缺乏研究而投反对票。
  • 错误信息是什么?
  • 标准HashSet 不知道您的自定义SetInterface。因此,您不能将SetInterface 的实例直接传递给HashSet 的方法并期望它神奇地做正确的事情。但是除了它不能处理你的参数参数之外,它也不会返回你所期望的。这就是您在遵循 Andreas 阅读文档的建议时所学到的,但阅读编译器的错误消息也会有所帮助。

标签: java generics hashset


【解决方案1】:
      interface SetInterface<T> {
      void add(T itemToAdd);
      public void remove( T itemToDelete);
    public boolean contains( T itemToCheck);
    public Set<T> makeUnion( SetInterface<T> otherSet);
    public Iterator<T> getIterator();
    public int size();
    HashSet<T> getSet();
      }

      public class Set<T> implements SetInterface<T>
    {
    private HashSet<T> set;
    public HashSet<T> getSet() {
    return set;
    }

    /**
     * Constructs a new empty set.
     */
    public Set () {
        set = new HashSet <>();
    }

    /**
     * Constructs a new set containing the elements in the specified collection. 
     * Default load factor of 0.75 and initial capacity of 50.
     * 
     * @param c- the collection whose elements are to be place into this set
     */
    public Set(Collection <? extends T> c) {
        set = new HashSet<>(Math.max((int) (c.size()/.75f) + 1, 50));
        set.addAll(c);
    }

    /**
     * Constructs a new empty set. Default load factor of 0.75.
     * 
     * @param initialCapacity- the initial capacity of the hash table
     */
    public Set(int initialCapacity) {
         set = new HashSet <>(initialCapacity);
    }

    /**
     * Constructs a new empty set. 
     * Hashmap has specified initial capacity and specified load factor.
     * 
     * @param initialCapacity- the initial capacity of the hash table
     *        loadFactor- the load factor of the hash map
     */
    public Set(int initialCapacity, float loadFactor) {
         set = new HashSet <>(initialCapacity, loadFactor);
    }

    /**
     * Add an item of type T to the interface  Duplicate items will not be
     * added to the set.
     * 
     * @param  itemToAdd - what to add.
     */
    public void add(T itemToAdd) {
        set.add(itemToAdd);
    }

    /**
     * Removes an item from the set ( if the item is in the set)  If the item is not
     * in the set this operation does nothing
     * 
     * @param  item to remove. 
     */
    public void remove( T itemToDelete) {
        set.remove(itemToDelete);
    }

    /**
     * Return if the SetInterface contains an item
     * 
     * @param itemToCheck.  The item you are looking for
     * @return  true if found.  False if not found.
     */
    public boolean contains( T itemToCheck) {
        return set.contains(itemToCheck);
    }

    /**
     * Make a union of two sets.  We add all items in either set to a new set and
     * return the new set.
     * 
     * @param the 'other' set to add to our set.
     * @return  A new set which is the union of the two sets. 
     */
    public Set<T> makeUnion( SetInterface<T> otherSet) {
    set.addAll(new java.util.ArrayList<T>(otherSet.getSet()));
        return this;
    }

    /** 
     * Return an iterator for the set.  This is used to walk thought all elements
     * in the set
     * 
     * @return  The iterator
     */
    public Iterator<T> getIterator() {
        return set.iterator();
    }

    /**
     * Tell the caller how many elements are in the set
     * 
     * @return int with the number of elements
     */
    public int size() {
        return set.size();
    }
    }

【讨论】:

  • 为什么是 arrayList 而不是 HashSet?
  • 也不能编译,因为 getSet() 不在给定的集合接口中。我们不允许编辑给定的界面。
  • 代码转储本身不是一个可接受的答案。您是否希望读者将您的代码与原始问题中的代码逐行进行比较?解释你改变了什么,以及为什么。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2012-05-09
  • 2019-10-19
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多