Created
August 7, 2023 15:13
-
-
Save ecourtial/32d79c4720771d482bb23199a5a4162e to your computer and use it in GitHub Desktop.
TEST - Java merge list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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