简体   繁体   中英

Need to create a datastructure similar to HashSet but should have frequency of elements contained

An array is there from which I need to pick the elements and store the element in the HashSet like structure as well as its frequency ie the number of times the element is there in the array.The other requirement is that the class should support operation of set intersection. What would be the better approach to do so :-

class myHashSet<E> extends AbstractSet<E> implements Set<E>// I dont think I need the serilizability and cloneability{
   //and then override the methods here
}

or

class myHashSet<E> exetnds HashSet<E>{

    // and then just override the add method like so
    public boolean More ...add(E e) {
        Integer count = map.get(temp);
        map.put(temp, (count == null) ? 1 : count + 1);
        return true;
   }
 }

or write my own class myHashSet without any reference to these preexisting classes.

My current understanding is that a wrapper Set can be enough which delegates all the interface calls to the wrapped Set instance and also maintains a map with the multiplicities. The multiplicity behavior is not clear to me, this is why I have put the TODO-s in the code with the multiplicity registry map.

public class MyHashSet<E> implements Set<E> {
    // element registration
    private final Set<E> mySet = new HashSet<E>();
    // multiplicity registration
    private final Map<E, Integer> myMap = new HashMap<E, Integer>();

    public int size() {
        // TODO Consider the role of myMap
        return mySet.size();
    }

    public boolean isEmpty() {
        // TODO Consider the role of myMap
        return mySet.isEmpty();
    }

    public boolean contains(Object o) {
        // TODO Consider the role of myMap
        return mySet.contains(o);
    }

    public Iterator<E> iterator() {
        // TODO Consider the role of myMap
        return mySet.iterator();
    }

    public Object[] toArray() {
        // TODO Consider the role of myMap
        return mySet.toArray();
    }

    public <T> T[] toArray(T[] a) {
        // TODO Consider the role of myMap
        return mySet.toArray(a);
    }

    public boolean add(E e) {
        // TODO Consider the role of myMap
        return mySet.add(e);
    }

    public boolean remove(Object o) {
        // TODO Consider the role of myMap
        return mySet.remove(o);
    }

    public boolean containsAll(Collection<?> c) {
        // TODO Consider the role of myMap
        return mySet.containsAll(c);
    }

    public boolean addAll(Collection<? extends E> c) {
        // TODO Consider the role of myMap
        return mySet.addAll(c);
    }

    public boolean retainAll(Collection<?> c) {
        // TODO Consider the role of myMap
        return mySet.retainAll(c);
    }

    public boolean removeAll(Collection<?> c) {
        // TODO Consider the role of myMap
        return mySet.removeAll(c);
    }

    public void clear() {
        // TODO Consider the role of myMap
        mySet.clear();
    }

    public boolean equals(Object o) {
        // TODO Consider the role of myMap
        return mySet.equals(o);
    }

    public int hashCode() {
        // TODO Consider the role of myMap
        return mySet.hashCode();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM