I have a List of String L1
I have another List of String L2
I want to copy only those items from L1 to L2 which are not already contained in L2
I know it might be quite easy but I could not find an easy solution
I have a List of String L1
I have another List of String L2
I want to copy only those items from L1 to L2 which are not already contained in L2
I know it might be quite easy but I could not find an easy solution
fill a set with the two lists, then convert the set back into a list (list2).
Set<String> set = new HashSet<String>(list2);
set.addAll(list1);
list2 = new ArrayList(set);
Take help form the below snippet. This will help.
List<String> l1 = new ArrayList<String>();
List<String> l2 = new ArrayList<String>();
l1.add("A");
l1.add("B");
l1.add("C");
l2.add("C");
l2.add("D");
l2.add("E");
Set<String> s = new HashSet<String>();
s.addAll(l1);
s.addAll(l2);
List<String> finalList = new ArrayList<String>();
finalList.addAll(s);
for (String str : finalList) {
System.out.println(str);
}
Something like
Set<String> stringSet = new HashSet<>(Arrays.asList(new String[]{"1","2","3"}));
stringSet.addAll(Arrays.asList(new String[]{"1","2","3","4"}));
List<String> finalList = new ArrayList<>(stringSet);