2

Using Java8 matching methods am able to compare two list and getting the boolean results when if the is any match is available in both the lists.

Please find my below code for that.

public class StreamTest2 {

    public static void main(String args[]) {

        List<Integer> aList = Arrays.asList( new Integer[] {
                1,3,5,6,8
        });

        List<Integer> bList = Arrays.asList( new Integer[] {
                10, 89, 8, 9
        });

        //If any number in List1 present in List2
        System.out.println("If any number present in aList is present in bList : "+aList.stream().anyMatch(num -> bList.contains(num)));

    }

}

Output :

If any number present in aList is present in bList : true

But, i want to print the matching number from both the list, how i can print the matching number ?

3
  • Why not just aList.retainAll(bList)? Commented Oct 28, 2018 at 12:35
  • @LuCio - It throws Exception in thread "main" java.lang.UnsupportedOperationException Exception Commented Oct 28, 2018 at 15:11
  • 1
    Yes, but only because you're using Arrays.asList(...). Initialize the lists like that new ArrayList<>(Arrays.asList(1, 3, 5, 6, 8)); Commented Oct 28, 2018 at 15:51

3 Answers 3

2

You can use filter and findFirst:

System.out.println("If any number present in aList is present in bList : "+aList.stream().filter(num -> bList.contains(num)).findFirst().orElse(null));

This will print the matching number if found (it will stop at the first match), or null, if no match is found.

Or, you can collect all the matches into a List:

System.out.println("If any number present in aList is present in bList : "+aList.stream().filter(num -> bList.contains(num)).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

2 Comments

Prefect, its working as per your note. But, if there is multiple matches means how we can print all the matches.?
@Karthikeyan Instead of terminating the stream pipeline with .findFirst(), you can collect it to a List (.collect(Collectors.toList()) which will contain all the matches.
1

Try this one

aList.stream().filter(bList::contains).collect(Collectors.toSet());

1 Comment

why not Collectors.toSet()? @Hadi
1

The reason because of which you get java.lang.UnsupportedOperationException when you call retainAll is that Arrays#asList returns an ArrayList backed by an array of fixed size. Any attempt to remove or add an element to these lists will result in the aforementioned UnsupportedOperationException.

The solution is, as @LuCio has suggested, to wrap the Arrays.asList part with a ArraysList constructor call, as such:

List<Integer> aList = new ArrayList(Arrays.asList(1,3,5,6,8));
List<Integer> bList = new ArrayList(Arrays.asList(10, 89, 8, 9));

the you should be able to call:

aList.retainAll(bList);

Keep in mind that this will modify the original aList list. If you need to preserve the state of aList then I would suggest to make a copy of aList before invoking retainAll. One of the suggested Stream API approached would in this case maybe be more suitable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.