Here we have chosen
LinkedHashSet because it preserves insertion order and also remove duplicate
elements
//Removing ArrayList elements
//Adding LinkedHashElements to ArrayList
Eg:
public static void
main(String[]args){
//creation of
Arraylist 1 with string arguments
//adding elements to
Arraylist 1
ArrayList<String>
al=new ArrayList<String>();
al.add("India");
al.add("US");
al.add("Australia");
al.add("Germany");
al.add("Germany");
al.add("Europe");
//Displaying elements
of Arraylist
System.out.println(al);
//creating Linked Hash
Set
LinkedHashSet<String>
lh=new LinkedHashSet<String>();
//adding Arraylist elements
to LinkedHashSet
lh.addAll(al);
//Removing ArrayList elements
al.clear();
//Adding LinkedHashElements to ArrayList
al.addAll(lh);
//Displaying elements
of Arraylist
System.out.println(al);
}
Output:[India,US,Australia,Germany,Germany,Europe]
//Non duplicates
Output:[India,US,Australia,Germany,Europe]
0 comments:
Post a Comment