Find diference between two ArrayLists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Find diference between two ArrayLists

I have to compare ArrayLst A and ArrayLst B . The result ArrayList should contain the List which does not exist in ArrayList A.

27th Sep 2016, 12:27 PM
Raphael
Raphael - avatar
1 Answer
+ 2
// Create a couple ArrayList objects and populate them // with some delicious fruits. Collection firstList = new ArrayList() {{ add("apple"); add("orange"); }}; Collection secondList = new ArrayList() {{ add("apple"); add("orange"); add("banana"); add("strawberry"); }}; // Show the "before" lists System.out.println("First List: " + firstList); System.out.println("Second List: " + secondList); // Remove all elements in firstList from secondList secondList.removeAll(firstList); // Show the "after" list System.out.println("Result: " + secondList);
27th Sep 2016, 12:31 PM
Andrew Williams
Andrew Williams - avatar