在Java 8中,保留两个不同集合中共同元素的更好方法是什么?

huangapple 未分类评论44阅读模式
英文:

What is a better way to retain common elements from two dissimilar collections in Java 8?

问题

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class HelloWorld {

    static class Example {
        UUID uuid;

        void setUuid(UUID uuid) {
            this.uuid = uuid;
        }

        UUID getUuid() {
            return this.uuid;
        }
    }

    public static void main(String[] args) {
        Example example1 = new Example();
        example1.setUuid(UUID.fromString("69320518-585f-43c8-a935-b6e0438042b7"));
        Example example2 = new Example();
        example2.setUuid(UUID.fromString("69320518-585f-43c8-a935-b6e0438043b7"));
        Example example3 = new Example();
        example3.setUuid(UUID.fromString("69320518-585f-43c8-a935-b6e0438044b7"));
        Example example4 = new Example();
        example4.setUuid(UUID.fromString("69320518-585f-43c8-a935-b6e0438045b7"));
        Example example5 = new Example();
        example5.setUuid(UUID.fromString("69320518-585f-43c8-a935-b6e0438046b7"));

        List<Example> exampleList = new ArrayList<>();
        exampleList.add(example1);
        exampleList.add(example2);
        exampleList.add(example3);
        exampleList.add(example4);
        exampleList.add(example5);

        List<UUID> uuidList = new ArrayList<>();
        uuidList.add(UUID.fromString("69320518-585f-43c8-a935-b6e0438047b7"));
        uuidList.add(UUID.fromString("69320518-585f-43c8-a935-b6e0438048b7"));
        uuidList.add(UUID.fromString("69320518-585f-43c8-a935-b6e0438049b7"));

        final List<Example> finalList = exampleList
                .stream()
                .filter(x -> uuidList.contains(x.getUuid()))
                .collect(Collectors.toList());
        System.out.println(finalList);

    }
}
英文:

I have two collections. One , an Arraylist of Objects where each object has a name and a UUID as variables. The second collection is an Arraylist of UUIDs. What is the best way to retain elements in the first arraylist where the first arraylist's UUIDs are part of the second UUID arraylist?

Example example1 = new Example();
example1.setUuid(uuid1);
Example example2 = new Example();
example2.setUuid(uuid2);
Example example3 = new Example();
example3.setUuid(uuid3);
Example example4 = new Example();
example4.setUuid(uuid4);
Example example5 = new Example();
example5.setUuid(uuid5);

List&lt;Example&gt; exampleList = new ArrayList&lt;&gt;();
exampleList.add(example1);
exampleList.add(example2);
exampleList.add(example3);
exampleList.add(example4);
exampleList.add(example5);

List&lt;UUID&gt; uuidList = new ArrayList&lt;&gt;();
uuidList.add(uuid1);
uuidList.add(uuid2);
uuidList.add(uuid3);

What is the best way to retain elements in exampleList containing only UUIDs from uuidList, which are uuid1, uuid2, uuid3, without doing a for loop? It would be a great to use stream and filter, but I wasn't able to get a grip of this.

final List&lt;Example&gt; finalList = exampleList
                    .stream()
                    .filter(x -&gt; !uuidList.contains(x))
                    .collect(Collectors.toList());

But this doesn't work at the moment, and I get a complain from the compiler that uuidList isn't effectively final. What's the best way to implement this , what is wrong in my implementation?

UPDATE: Minimum Reproducible Example:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

public class HelloWorld{


static class Example {
    UUID uuid;
    
    void setUuid(UUID uuid){
        this.uuid = uuid;
    }
    
    UUID getUuid(){
        return this.uuid;
    }
}

 public static void main(String []args){
    Example example1 = new Example();
    example1.setUuid(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438042b7&quot;));
    Example example2 = new Example();
    example2.setUuid(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438043b7&quot;));
    Example example3 = new Example();
    example3.setUuid(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438044b7&quot;));
    Example example4 = new Example();
    example4.setUuid(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438045b7&quot;));
    Example example5 = new Example();
    example5.setUuid(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438046b7&quot;));
    
    List&lt;Example&gt; exampleList = new ArrayList&lt;&gt;();
    exampleList.add(example1);
    exampleList.add(example2);
    exampleList.add(example3);
    exampleList.add(example4);
    exampleList.add(example5);
    
    List&lt;UUID&gt; uuidList = new ArrayList&lt;&gt;();
    uuidList.add(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438047b7&quot;));
    uuidList.add(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438048b7&quot;));
    uuidList.add(UUID.fromString(&quot;69320518-585f-43c8-a935-b6e0438049b7&quot;));
    
    final List&lt;Example&gt; finalList = exampleList
            .stream()
            .filter(x -&gt; !uuidList.contains(x.getUuid()))
            .collect(Collectors.toList());
    System.out.println(finalList);

 }
}

huangapple
  • 本文由 发表于 2020年4月8日 23:13:53
  • 转载请务必保留本文链接:https://java.coder-hub.com/61104060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定