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 ?
aList.retainAll(bList)?Exception in thread "main" java.lang.UnsupportedOperationExceptionExceptionArrays.asList(...). Initialize the lists like thatnew ArrayList<>(Arrays.asList(1, 3, 5, 6, 8));