You can get common items between two lists using the "retainAll" method. This method will remove all unmatched items from the list to which it applies.
Ex.: list.retainAll(list1);
In this case, all elements that are not in list1 will be removed from the list and only those elements that are common to list and list1 will remain.
List<Integer> list = new ArrayList<>(); list.add(10); list.add(13); list.add(12); list.add(11); List<Integer> list1 = new ArrayList<>(); list1.add(10); list1.add(113); list1.add(112); list1.add(111);
Exit:
[10, 13, 12, 11] [10, 113, 112, 111] list::[10] list1::[10, 113, 112, 111]
NOTE. After applying retainAll to the list, the list contains a common element between list and list1.
Vivek Kumar Sihare May 21 '18 at 8:04 am 2018-05-21 08:04
source share