Skip to content

Instantly share code, notes, and snippets.

@ecourtial
Created August 7, 2023 15:13
Show Gist options
  • Save ecourtial/32d79c4720771d482bb23199a5a4162e to your computer and use it in GitHub Desktop.
Save ecourtial/32d79c4720771d482bb23199a5a4162e to your computer and use it in GitHub Desktop.
TEST - Java merge list
import java.util.LinkedList;
public class main
{
public static void main(String[] args)
{
// 1) Create the two integer list
LinkedList<Integer> firstList = new LinkedList<>();
firstList.add(1);
firstList.add(2);
firstList.add(3);
firstList.add(5);
LinkedList<Integer> secondList = new LinkedList<>();
secondList.add(2); // Duplicate
secondList.add(4);
secondList.add(6);
// 2) Parse the first one and check->clear the second one if the occurence exists in the other list
firstList.forEach((temp) -> {
int indexOf = secondList.indexOf(temp);
if (indexOf != -1) {
secondList.remove(indexOf);
}
});
// 3) Merge
LinkedList<Integer> mergedList = new LinkedList<>();
mergedList.addAll(firstList);
mergedList.addAll(secondList);
// 4) Display result
System.out.println("firstList : "+firstList);
System.out.println("secondList : "+secondList);
System.out.println("mergedList : "+mergedList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment