Generally, sets are a type of collection which stores unique values. Uniqueness is determined by the equals() and hashCode() methods.
Sorting is determined by the type of set.
HashSet - Random Sorting
Version ≥ Java SE 7 Set<String> set = new HashSet<> (); set.add("Banana"); GoalKicker.com – Java® Notes for Professionals 159 set.add("Banana"); set.add("Apple"); set.add("Strawberry"); // Set Elements: ["Strawberry", "Banana", "Apple"]
LinkedHashSet - Insertion Order
Version ≥ Java SE 7 Set<String> set = new LinkedHashSet<> (); set.add("Banana"); set.add("Banana"); set.add("Apple"); set.add("Strawberry"); // Set Elements: ["Banana", "Apple", "Strawberry"]
TreeSet - By compareTo() or Comparator
Version ≥ Java SE 7 Set<String> set = new TreeSet<> (); set.add("Banana"); set.add("Banana"); set.add("Apple"); set.add("Strawberry"); // Set Elements: ["Apple", "Banana", "Strawberry"]
Version ≥ Java SE 7 Set<String> set = new TreeSet<> ((string1, string2) -> string2.compareTo(string1)); set.add("Banana"); set.add("Banana"); set.add("Apple"); set.add("Strawberry"); // Set Elements: ["Strawberry", "Banana", "Apple"]
Using a new List
List<String> list = new ArrayList<String>(listOfElements);
Using List.addAll() method
Set<String> set = new HashSet<String>(); set.add("foo"); set.add("boo"); List<String> list = new ArrayList<String>(); list.addAll(set);
Using Java 8 Steam API
List<String> list = set.stream().collect(Collectors.toList());
Suppose you have a collection elements, and you want to create another collection containing the same elements but with all duplicates eliminated:
Collection<Type> noDuplicates = new HashSet<Type>(elements);
Example:
List<String> items = new ArrayList<>( Arrays.asList("Alice", "Bob", "Charlie", "Alice", "Eve", "Charlie", "Bob")); Set<String> uniqueItems = new HashSet<>(items); System.out.println("uniqueItems = " + uniqueItems);
Output:
uniqueItems = [Alice, Bob, Charlie, Eve]
You can create a new class that inherits from HashSet:
Set<String> h = new HashSet<String>() {{ add("a"); add("b"); }};
One line solution:
Set<String> h = new HashSet<String>(Arrays.asList("a", "b"));
Using guava:
Sets.newHashSet("a", "b", "c")
Using Streams:
Set<String> set3 = Stream.of("a", "b", "c").collect(toSet());
What are differences between List and Set collection at the top level and How to choose when to use List in java and when to use Set in Java
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class DifferentSetAndListExample { public static void main(String[] args) { System.out.println("List example ....."); List<String> myList = new ArrayList<>(); myList.add("A"); myList.add("B"); myList.add("C"); myList.add("D"); myList.add("A"); for (String item : myList) { System.out.println(item); } System.out.println("Set example ....."); Set<String> mySet = new HashSet<>(); mySet.add("X"); mySet.add("Y"); mySet.add("Z"); mySet.add("W"); mySet.add("X"); mySet.add("Y"); mySet.add("K"); for (String item : mySet) { System.out.println(item); } } }
Output:
List example ..... A B C D A Set example ..... K X Y Z W
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions