在一个HashSet使用或和与方法(Using union and intersection met

2019-10-18 00:34发布

我用我的自定义设置,MYSET有麻烦,使用工会的基本功能和相交。 程序编译时没有错误,但只返回一个空集。

任何人看到这个问题?

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


}

主要方法

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

    }
}

Answer 1:

看来你试图实现组成,并在同一时间延长树集,但是这是不是一个好的做法,你要么使用成分并实现Set接口(带有一个TreeSet后端),要么扩展树集

扩展TreeSet中

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

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

使用成分

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


文章来源: Using union and intersection method in a HashSet