英文:
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<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(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<Example> finalList = exampleList
.stream()
.filter(x -> !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("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);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论