Given 2 lists List<Group> old, List<Group> new Write the for loop so that it iterates through the union of two lists and 1. Checks if group is present in both lists 2. If true, it checks whether group Names are same by ignoring the case. 3. Increases counter by 1, if both conditions are true. Iterate only through the union of two lists
public static void main(String[] args) { List<Group> old = new ArrayList<>(); List<Group> newList = new ArrayList<>(); old.add(new Group("a")); old.add(new Group("b")); newList.add(new Group("a")); newList.add(new Group("c")); System.out.println(check(old, newList)); } public static int check(List<Group> old, List<Group> newList) { int i = 0; Set<Group> union = new HashSet<>(old); union.addAll(newList); for (Group group : union) { if (old.contains(group) && newList.contains(group)) { if (old.get(old.indexOf(group)).getName().equalsIgnoreCase(newList.get(newList.indexOf(group)).getName())) { i++; } } }