I have 2 List<xxx>
and need to intersection them by xxx.getId()
, so I use Stream
and Lambda
. These steps are:
List<xxx> list1 = function1();
List<xxx> list2 = function2();
List<String> list1InString = list1.stream().map(input -> input.getId()).collect(toList());
list2 = list2.stream().filter(input -> list1InString.contains(input.getId())).collect(toList());
I want to do it by using double colon
like:
list2 = list2.stream().filter(list1InString::contains).collect(toList());
But it's not true because contains
reference to xxx object not String getId() method. I know that I waste time to convert list1 to list1InString, but I don't know any better method. Any help is very precious, thanks so much.
Edit: Thanks for many answer, I just wanna make code more simple by using double colon (which is new for me), but in this case may not use. Special thanks to Naman and Holger for clarifying my problem very much.