Click here to Skip to main content
15,887,449 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My assignment is to create a generic Set class which implements the given SetInterface. I have decided to use HashSet but I'm not exactly sure how that works.

What I have tried:

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();
    }
}
Posted
Updated 30-Nov-16 22:37pm
Comments
PIEBALDconsult 1-Dec-16 0:33am    
I don't see how a simple wrapper around an existing class will satisfy the assignment.
Member 12880253 1-Dec-16 0:36am    
We just have to code the methods ourselves...
PIEBALDconsult 1-Dec-16 0:51am    
I wrote a Set class in C# years ago. It's basically obsolete now that .net has HashSet.
https://www.codeproject.com/Articles/16459/A-Set-class

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900