如何在HashSet中使用addAll和retainAll方法

时间:2016-12-01 21:44:23

标签: java generics hashset

当我尝试编译时,我收到了一个关于makeUnion方法的错误消息,我猜我也会得到一个makeIntersection。如果我想为新集添加一个set接口,我不知道为什么会这样,或者如何实现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();
}

}

1 个答案:

答案 0 :(得分:-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();
    }
    }