Using union and intersection method in a HashSet

隐身守侯 提交于 2019-12-11 10:29:41

问题


I am having trouble using my custom set,MySet, using the basic function of union and intersecting. The program compiles without error but just returns an empty set.

Anybody see where the problem is?

public class MySet<E> extends TreeSet<E> {
    Set<E> set;

    public MySet(){
        set = null;
    }

    public MySet(Set<E> set){
        this.set = set;
    }

    public void union(Set<E> s){
        set.addAll(s);
    }

    public void intersection(Set<E> s){ 
        set.retainAll(s);
    }


}

Main method

public class TestSet {

    public static void main(String[] args) throws FileNotFoundException{
        File f1 = new File("courseList1.txt");
        File f2 = new File("courseList2.txt");

        Scanner scan1 = new Scanner(f1);
        Scanner scan2 = new Scanner(f2);

        Set<Coarse> set1 = new HashSet<Coarse>();
        Set<Coarse> set2 = new HashSet<Coarse>();

        MySet<Coarse> mySet = new MySet<Coarse>(set1);

        String designator;
        int number;

        while(scan1.hasNext()){
            designator = scan1.next();
            number = scan1.nextInt();
            set1.add(new Coarse(designator, number));
        }

        while(scan2.hasNext()){
            designator = scan2.next();
            number = scan2.nextInt();
            set2.add(new Coarse(designator, number));
        }

        mySet.union(set2);
        mySet.intersection(set2);

    }
}

回答1:


It seems that you are trying to implement composition and at the same time extending the tree set, but that is not a good practice, you either use composition and implement the Set interface (backend with a TreeSet) or extends the tree set

Extending the TreeSet

    class MySet<E> extends TreeSet<E> {
        public void union(Set<E> s){
            addAll(s);
        }

        public void intersection(Set<E> s){
            retainAll(s);
        }
    }

using composition

    class MySet<E> implements Set<E> {
        private TreeSet<E> set;

        public MySet(TreeSet<E> set) {
            this.set = new TreeSet<>(set);
        }

        public void union(Set<E> s){
            set.addAll(s);
        }

        public void intersection(Set<E> s){
            set.retainAll(s);
        }

        @Override
        public int size() {
            return set.size();
        }

        @Override
        public boolean isEmpty() {
            return set.isEmpty();
        }

        @Override
        public boolean contains(Object o) {
            return set.contains(o);
        }

        @Override
        public Iterator<E> iterator() {
            return set.iterator();
        }

        @Override
        public Object[] toArray() {
            return set.toArray();
        }

        @Override
        public <T> T[] toArray(T[] a) {
            return set.toArray(a);
        }

        @Override
        public boolean add(E e) {
            return set.add(e);
        }

        @Override
        public boolean remove(Object o) {
            return set.remove(o);
        }

        @Override
        public boolean containsAll(Collection<?> c) {
            return set.containsAll(c);
        }

        @Override
        public boolean addAll(Collection<? extends E> c) {
            return set.addAll(c);
        }

        @Override
        public boolean retainAll(Collection<?> c) {
            return set.retainAll(c);
        }

        @Override
        public boolean removeAll(Collection<?> c) {
            return set.removeAll(c);
        }

        @Override
        public void clear() {
            set.clear();
        }

        @Override
        public boolean equals(Object o) {
            return set.equals(o);
        }

        @Override
        public int hashCode() {
            return set.hashCode();
        }
    }


来源:https://stackoverflow.com/questions/15931427/using-union-and-intersection-method-in-a-hashset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!