Reputation: 7011
In Java, we can use variadic function in the following way:
public Set packStrings(String...strings){
for (String str : strings){
//Do something on the str
}
//How to convert strings into a Set?
}
My question is, what is the type of "strings"? is it String[]? How to put the string(s) denoted by the "strings" into Java Collection, such as Set or List?
Please kindly advise.
Thanks & regards, William Choi
Upvotes: 1
Views: 1847
Reputation: 240908
public Set<String> packStrings(String...strings){
//Way 1 to create List
List<String> strList = new ArrayList<String>();
for (String str : strings){
//adding to list
strList.add(str);
}
//way 2 to create List
List<String> strList = Arrays.asList(strings);
//converting to set from List
Set<String> setStr= new HashSet<String>(strList);
return setStr;
}
Have a look at this Doc
Upvotes: 2
Reputation: 7403
It's a string array, so you can do:
Arrays.asList(strings);
Upvotes: 3