Member 12880253 Ответов: 1

Писать собственные реализации в Java


Мое задание состоит в том, чтобы создать универсальный класс Set, который реализует данный SetInterface. Я решил использовать HashSet, но я не совсем уверен, как это работает.

Что я уже пробовал:

public class Set<t> implements SetInterface<t>
{
    private HashMap<t,object> map;
    private static final Object x = new Object();
    
    /**
     * Constructs a new empty set.
     */
    public Set () {
        map = new HashMap <>();
    }
    
    /**
     * 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  c) {
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 50));
        //add(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) {
        map = new HashMap<>(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) {
        map = new HashMap<>(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) {
        map.put(itemToAdd,null);
    }
    
    /**
     * 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) {
        map.remove(itemToDelete,x);
    }

    /**
     * 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 map.containsKey(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 x = new Set();
        
    }

    /**
     * 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) {
        Set x = new Set();
        
    }

    /** 
     * 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 map.keySet().iterator();
    }

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

PIEBALDconsult

Я не вижу, как простая оболочка вокруг существующего класса удовлетворит заданию.

Member 12880253

Мы просто должны сами кодировать методы...

PIEBALDconsult

Я написал класс Set на C# много лет назад. Теперь, когда .net имеет HashSet, он практически устарел.
https://www.codeproject.com/Articles/16459/A-Set-class

1 Ответов

Рейтинг:
0

Richard MacCutchan

Видеть HashSet (Java Platform SE 7 )[^].