I want to initialize a Set Implementation (HashSet) in Java with an Iterable. However, the constructor of HashSet doesn't accept Iterables, but only Collections type objects.
Is there a way to convert from Iterable to some subtype of Collections.
Initialize a Set You can initialize an empty set by using set() . To intialize a set with values, you can pass in a list to set() . If you look at the output of dataScientist and dataEngineer variables above, notice that the values in the set are not in the order added in. This is because sets are unordered.
Initialization Using the Default Constructor One standard way to initialize a set is to initialize using the default constructor, this will generate an empty set. Elements can be added to it using an inbuilt set. insert() method. Here, the insert() method can be further used to insert elements into the set.
Use Set type and new keyword to create a Set in typescript. let directions = new Set<string>(); To create a Set with initial values we can pass the values in an array.
You can use Guava.
Set<T> set = Sets.newHashSet(iterable);
or to make it read like a sentence static import,
import static com.google.common.collect.Sets.*; Set<T> set = newHashSet(iterable);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With