Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a Set with an Iterable

Tags:

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.

like image 504
VaidAbhishek Avatar asked May 08 '13 20:05

VaidAbhishek


People also ask

What is the correct way to initialize a set in Python?

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.

How do you initialize a set in C ++?

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.

How do you initialize a set in typescript?

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.


1 Answers

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); 
like image 107
ConnorWGarvey Avatar answered Oct 31 '22 07:10

ConnorWGarvey