This has been confusing be quite a bit! Sorry if the explanation is bad. I am practicing Java streams using Sets. I have a custom type PartiallyOrderedSet
initialized like this:
PartiallyOrderedSet four = makeSet(new Integer[] { 1, 2, 3, 4 });
Internally, it stores a collection of generic E, so that makeSet
can take any type E.
To create the PartiallyOrderedSet, we use makeSet like this, for integers for example (though we could use multiple overloads of makeSet to get it to any type):
private static PartiallyOrderedSet<Int> makeSet(Integer[] elts) {
return new PartiallyOrderedSet<Int>(
Arrays.asList(elts).stream().map(Int::new).collect(Collectors.toList()));
}
To reiterate, inside of PartiallyOrderedSet, I have a private field called elements
, which is <E>
generic. This stores the internal objects of the custom object:
private Collection<E> elements;
I want to convert it back to a java Set<E>
object so that I can print the internal elements of the collection of elements. I want to do this using Java streams, though I'm quite lost. Could anyone lend a hand?
The output should for example look like this:
{1,2,3,4}
for PartiallyOrderedSet four = makeSet(new Integer[] { 1, 2, 3, 4 });