在java中编写自己的set实现 [英] Write own set implementation in java

查看:67
本文介绍了在java中编写自己的set实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是创建一个实现给定SetInterface的通用Set类。我已经决定使用HashSet,但我不确定它是如何工作的。



我尝试过:



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

推荐答案

参见 HashSet(Java Platform SE 7) [ ^ ]。


这篇关于在java中编写自己的set实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆