I am trying to \"combine\" two arrayLists, producing a new arrayList that contains all the numbers in the two combined arrayLists, but without any duplicate elements and the
Add elements in first arraylist
ArrayList firstArrayList = new ArrayList();
firstArrayList.add("A");
firstArrayList.add("B");
firstArrayList.add("C");
firstArrayList.add("D");
firstArrayList.add("E");
Add elements in second arraylist
ArrayList secondArrayList = new ArrayList();
secondArrayList.add("B");
secondArrayList.add("D");
secondArrayList.add("F");
secondArrayList.add("G");
Add first arraylist's elements in second arraylist
secondArrayList.addAll(firstArrayList);
Assign new combine arraylist and add all elements from both arraylists
ArrayList comboArrayList = new ArrayList(firstArrayList);
comboArrayList.addAll(secondArrayList);
Assign new Set for remove duplicate entries from arraylist
Set setList = new LinkedHashSet(comboArrayList);
comboArrayList.clear();
comboArrayList.addAll(setList);
Sorting arraylist
Collections.sort(comboArrayList);
Output
A
B
C
D
E
F
G