java create new set code example
Example 1: java create a set with values
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));
Example 2: how to initialize a set in java
Set<E> h = new HashSet<E>();
Example 3: java how to make set
HashSet<String> set = new HashSet();
set.add("One");
set.add("Two");
set.remove("One");
set.clear();
Example 4: how to check how many elements in a set java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class MainClass {
public static void main(String[] a) {
String elements[] = { "A", "B", "C", "D", "E" };
Set set = new HashSet(Arrays.asList(elements));
System.out.println(set.size());
}
}