Variations
- Insertion Order must be preserved.
- Insertion Order need not be preserved,
- Inserted data is an Object
Code
public class Test2 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Tyson");
list.add("Justin");
list.add("Justin");
list.add("Martin");
Set<String> set2 = new HashSet<String>(list); //Variation #1 If insertion order is not suppose to be preserved
System.out.println(set2);
Set<String> set1 = new LinkedHashSet<String>(list); //Variation # 2If insertion order is suppose to be preserved
System.out.println(set1);
list.clear();
list.addAll(set2); //All the data is
}
}