you can do something like this
CopyString[] item = s.toArray(new String[s.size()]);
As per Java doc - toArray function ( which is the one you need )
Answer from Maciej Cygan on Stack OverflowtoArray() Returns an array containing all of the elements in this list in proper sequence (from first to last element).
you can do something like this
CopyString[] item = s.toArray(new String[s.size()]);
As per Java doc - toArray function ( which is the one you need )
toArray() Returns an array containing all of the elements in this list in proper sequence (from first to last element).
You do not have to convert a Set to an array just to operate on the elements. You can iterate over the elements directly
CopySet<String> s = new HashSet<>();
....
for (String item : s)
{
do something...
}
Videos
Use the Set#toArray(IntFunction<T[]>) method taking an IntFunction as generator.
String[] GPXFILES1 = myset.toArray(String[]::new);
If you're not on Java 11 yet, then use the Set#toArray(T[]) method taking a typed array argument of the same size.
String[] GPXFILES1 = myset.toArray(new String[myset.size()]);
While still not on Java 11, and you can't guarantee that myset is unmodifiable at the moment of conversion to array, then better specify an empty typed array.
String[] GPXFILES1 = myset.toArray(new String[0]);
Java 11
The new default toArray method in Collection interface allows the elements of the collection to be transferred to a newly created array of the desired runtime type. It takes IntFunction<T[]> generator as argument and can be used as:
String[] array = set.toArray(String[]::new);
There is already a similar method Collection.toArray(T[]) and this addition means we no longer be able to pass null as argument because in that case reference to the method would be ambiguous. But it is still okay since both methods throw a NPE anyways.
Java 8
In Java 8 we can use streams API:
String[] array = set.stream().toArray(String[]::new);
We can also make use of the overloaded version of toArray() which takes IntFunction<A[]> generator as:
String[] array = set.stream().toArray(n -> new String[n]);
The purpose of the generator function here is to take an integer (size of desired array) and produce an array of desired size. I personally prefer the former approach using method reference than the later one using lambda expression.
Like this:
Set<T> mySet = new HashSet<>(Arrays.asList(someArray));
In Java 9+, if unmodifiable set is ok:
Set<T> mySet = Set.of(someArray);
In Java 10+, the generic type parameter can be inferred from the arrays component type:
var mySet = Set.of(someArray);
Be careful
Set.of throws IllegalArgumentException - if there are any duplicate elements in someArray. See more details: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Set.html#of(E...)
If you want an unmodifiable set and you might have duplicate elements in the array, do the following:
var mySet = Set.copyOf(Arrays.asList(array));
Set<T> mySet = new HashSet<T>();
Collections.addAll(mySet, myArray);
That's Collections.addAll(java.util.Collection, T...) from JDK 6.
Additionally: what if our array is full of primitives?
For JDK < 8, I would just write the obvious for loop to do the wrap and add-to-set in one pass.
For JDK >= 8, an attractive option is something like:
Arrays.stream(intArray).boxed().collect(Collectors.toSet());
i wonder how i could make a set method for an array
mySet = Arrays.stream(line
.split(" "))
.map(String::toCharArray)
.map(Set::of)
.toArray(i -> new Set[i]);I don't know much about stream but I'm trying to use it to convert a sentence to an array of sets where each set is a set of the characters in each word.
For example, a sentence, "Hello human being" would turn to an array of three sets containing
('H', 'e', 'l', 'l', 'o')
('h', 'u', 'm', 'a', 'n')
and
('b', 'e', 'i', 'n', 'g')I could brute force it by doing
int cntr = 0;
for (String word: sentence.split(" ")){
setArray[cntr] = new HashSet<Character>();
for (char letter: word.toCharArray())
valuesBeforeAnalysisSet[cntr].add(letter);
cntr++;
}But how can I do it elegantly?