Java 三个给定集合中两个集合的交集?

Java 三个给定集合中两个集合的交集?,java,set,Java,Set,我必须写一个算法,从三个给定的集合中找出两个集合的交集。例如,如果元素存在于三个集合中的两个集合中,则该元素是好的,但如果元素存在于所有三个集合中,则不应添加该元素 创建哈希映射 迭代每个集合,如果key不在hashmap中,则将元素添加为key,值为1,如果key不在hashmap中,则将值增加1 迭代hashmap并使用值为2的键创建结果集 一个好的解决方案是收集所有集合,计算每个元素的频率,只保留出现两次的元素 使用流 一条有3套的小溪 连接的3个集合的所有元素的流 绘制地图 成对迭代

我必须写一个算法,从三个给定的集合中找出两个集合的交集。例如,如果元素存在于三个集合中的两个集合中,则该元素是好的,但如果元素存在于所有三个集合中,则不应添加该元素

  • 创建哈希映射
  • 迭代每个集合,如果key不在hashmap中,则将元素添加为key,值为1,如果key不在hashmap中,则将值增加1
  • 迭代hashmap并使用值为2的键创建结果集

  • 一个好的解决方案是收集所有集合,计算每个元素的频率,只保留出现两次的元素

    使用流

    • 一条有3套的小溪
    • 连接的3个集合的所有元素的流
    • 绘制地图
    • 成对迭代
    • 保留值==2的
    • 只保留钥匙
    • 集合
    static Set intersection两个但不是三个(Set s1、Set s2、Set s3){
    返回流(s1、s2、s3)
    .flatMap(集合::流)
    .collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
    .entrySet()
    .stream()
    .filter(条目->条目.getValue()==2)
    .map(map.Entry::getKey)
    .collect(收集器.toSet());
    }
    
    带循环

    static <T> Set<T> intersectionTwoButNotThree(Set<T> s1, Set<T> s2, Set<T> s3) {
        Map<T, Integer> map = new HashMap<>();
        for (T elt : s1) 
            map.merge(elt, 1, Integer::sum); // sum previous value and 1
        for (T elt : s2) 
            map.merge(elt, 1, Integer::sum); // sum previous value and 1
        for (T elt : s3) 
            map.merge(elt, 1, Integer::sum); // sum previous value and 1
    
        Set<T> set = new HashSet<>();
        for (Map.Entry<T, Integer> e : map.entrySet()) 
            if (e.getValue() == 2) 
                set.add(e.getKey());
    
        return set;
    }
    
    static Set intersection两个但不是三个(Set s1、Set s2、Set s3){
    Map Map=newhashmap();
    对于(英语:s1)
    merge(elt,1,Integer::sum);//求上一个值和1的和
    对于(英语:s2)
    merge(elt,1,Integer::sum);//求上一个值和1的和
    英语四级考试(英语三级)
    merge(elt,1,Integer::sum);//求上一个值和1的和
    Set=newhashset();
    对于(Map.Entry e:Map.entrySet())
    如果(如getValue()==2)
    set.add(例如getKey());
    返回集;
    }
    
    下面是我的尝试

    public class Test {
    
        List<Set> sets = new ArrayList<>();
        Set<String> one = new HashSet<>();
        Set<String> two = new HashSet<>();
        Set<String> thr = new HashSet<>();
    
        enum EStatus{
            IT_IS_GOOD,
            SHOULD_NOT_BE_ADDED,
            EPIC_FAIL
        }
    
        public static void main(String[] args) {
            Test mainT = new Test();
            mainT.prepareData();
            System.out.println(mainT.doStuff("three"));
        }
    
    
        public EStatus doStuff(String element){
            for(int i = 1; i <= 3; i++){
                Set<String> intersectionOfTwo = new HashSet<>(sets.get(i-1));
                intersectionOfTwo.retainAll(sets.get(i % 3));
                if(intersectionOfTwo.contains(element)){
                    Set<String> intersectionOfThree  = new HashSet<>(intersectionOfTwo);
                    intersectionOfThree.retainAll(sets.get((i + 1) % sets.size()));
                    if(intersectionOfThree.contains(element)){
                        return EStatus.SHOULD_NOT_BE_ADDED;
                    }else{
                        return EStatus.IT_IS_GOOD;
                    }
                }
            }
            return EStatus.EPIC_FAIL;
        }
    
        public void prepareData(){
            one.add("one");
            one.add("two");
            one.add("three");
            two.add("two");
            two.add("three");
            thr.add("three");
            thr.add("four");
            sets.add(one);
            sets.add(two);
            sets.add(thr);
        }
    }
    
    公共类测试{
    列表集=新的ArrayList();
    Set one=新的HashSet();
    Set two=新的HashSet();
    Set thr=新的HashSet();
    财产清单{
    这很好,
    如果不加上,,
    史诗般的失败
    }
    公共静态void main(字符串[]args){
    测试维护=新测试();
    维护准备数据();
    系统输出打印号(mainT.doStuff(“三”);
    }
    公共资产(字符串元素){
    
    对于(int i=1;i)你的问题是什么?嗨,马克!欢迎来到StackOverflow,这个问题太广泛了,请查看以了解如何改进你的问题。
    public class Test {
    
        List<Set> sets = new ArrayList<>();
        Set<String> one = new HashSet<>();
        Set<String> two = new HashSet<>();
        Set<String> thr = new HashSet<>();
    
        enum EStatus{
            IT_IS_GOOD,
            SHOULD_NOT_BE_ADDED,
            EPIC_FAIL
        }
    
        public static void main(String[] args) {
            Test mainT = new Test();
            mainT.prepareData();
            System.out.println(mainT.doStuff("three"));
        }
    
    
        public EStatus doStuff(String element){
            for(int i = 1; i <= 3; i++){
                Set<String> intersectionOfTwo = new HashSet<>(sets.get(i-1));
                intersectionOfTwo.retainAll(sets.get(i % 3));
                if(intersectionOfTwo.contains(element)){
                    Set<String> intersectionOfThree  = new HashSet<>(intersectionOfTwo);
                    intersectionOfThree.retainAll(sets.get((i + 1) % sets.size()));
                    if(intersectionOfThree.contains(element)){
                        return EStatus.SHOULD_NOT_BE_ADDED;
                    }else{
                        return EStatus.IT_IS_GOOD;
                    }
                }
            }
            return EStatus.EPIC_FAIL;
        }
    
        public void prepareData(){
            one.add("one");
            one.add("two");
            one.add("three");
            two.add("two");
            two.add("three");
            thr.add("three");
            thr.add("four");
            sets.add(one);
            sets.add(two);
            sets.add(thr);
        }
    }