【问题标题】:Initialise Power set as anonymous interface初始化电源设置为匿名接口
【发布时间】:2020-05-22 20:08:51
【问题描述】:
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PowerSet {

    public static final <E> Collection<Set<E>> of(Set<E> s) {
        List<E> src = new ArrayList<>(s);
        if (src.size() > 30) {
            throw new IllegalArgumentException("Set too big " + s);
        }
        return new AbstractList<Set<E>>() {
            @Override
            public int size() {
                return 1 << src.size(); // 2 to the power srcSize
            }

            @Override
            public boolean contains(Object o) {
                return o instanceof Set && src.containsAll((Set) o);
            }

            @Override
            public Set<E> get(int index) {
                Set<E> result = new HashSet<>();
                for (int i = 0; index != 0; i++, index >>= 1) {
                    if ((index & 1) == 1) {
                        result.add(src.get(i));
                    }
                }
                return result;
            }
        };


    }

    public static void main(String[] args) {
        Collection<Set<String>> set = new HashSet<>();
        set.add()...    }

我有这段代码是我从 Java 获得的,它是如何实现的 电源组,但我很困惑如何初始化这个组和 用值填充它。有三个覆盖的接口 方法,具体包含、get和size。在课堂上做什么 声明是什么意思?

【问题讨论】:

    标签: java collections powerset


    【解决方案1】:

    要初始化 PowerSet 实例,您需要调用它的构造函数 PowerSet(),尽管它没有用,因为其中没有与对象相关的底层逻辑。

    "of" 是 PowerSet 中声明的静态方法,接受大小 power set 的集合列表。 它是通过AbstractList的扩展实现的。

    Set<Long> input = new HashSet<Long>();
    input.add(1L);
    input.add(2L);
    List<Set<Long>> example = PowerSet.<Long>of(input);
    

    所以,example.get(0) 将产生一个空的 HashSet,example.get(1) == {1}; example.get(2) == {2}, example.get(3) == {1, 2}

    你可以用更传统的格式重写它:

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class PowerSet<E> extends AbstractList<Set<E>> { 
    
        private List<E> src;
    
        public PowerSet(Set<E> s){
           //copying set contains to a list to access by index
           src = new ArrayList<>(s);
        } 
        @Override
        public int size() {
            return 1 << src.size(); // 2 to the power srcSize
        }
    
        @Override
        public boolean contains(Object o) {
            return o instanceof Set && src.containsAll((Set) o);
        }
    
        @Override
        public Set<E> get(int index) {
            Set<E> result = new HashSet<>();
            for (int i = 0; index != 0; i++, index >>= 1) {
                if ((index & 1) == 1) {
                     result.add(src.get(i));
                }
            }
            return result;
        }
    
        public static void main(String[] args) {
            Set<String> set = new HashSet<>();
            set.add()...  
            PowerSet<String> = new PowerSet(set);  
    
    }
    

    【讨论】:

    • 第一个代码示例给了我错误,它在 example.get(0) 等上找不到 get 方法。第二个代码给我的不是抽象的,也没有覆盖方法 get(int ) 类型不兼容。
    • 将 public static final Collection> of(Set s) 更改为 public static final List> of(Set s) 用于原始示例。
    • 或者在第二个示例中更改 public class PowerSet extends AbstractList> (我已经更新了上面的代码)。如果您仍有问题,请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多