-1

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 });

  • 2
    What is `Int`? Why did you create such a class? Isn't `Integer` good enough for you? – Andreas Apr 13 '21 at 01:55
  • 1
    Why do you call the class `PartiallyOrderedSet` when the underlying data is a `Collection` (actually `List` in this example), in other words, neither ordered nor a set? Besides that, it’s not clear where the problem is. You’ve shown to know how to transform with a stream. Is it the challenge to use `elements.stream()` to create the stream from a collection instead of an array? Or is to use `collect(Collectors.toSet()))` to create a `Set` instead of a `List`? – Holger Apr 14 '21 at 08:57

2 Answers2

0

You could try:-

Set myset = elements.stream().collect(Collectors.toSet()));

Code is untested, however.

Preeti
  • 222
  • 1
  • 8
  • Thanks for the reply. For reference, this returns ```[src.Test$Int@42f30e0a]```, which is the internal object. (src is package inside Test). I'm not sure if I need to convert it again or something. Also I believe collect might be returning a list? Not sure. I think it should have ```{``` brackets. – user3236779 Apr 13 '21 at 01:46
  • @user3236779 See: [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/q/29140402/5221149) – Andreas Apr 13 '21 at 01:55
  • Did you try calling myset.tostring() – Preeti Apr 13 '21 at 14:01
0

Should be like this, you can find information about generics right here

private static <T> PartiallyOrderedSet<T> makeSet(T[] elts) {
    return new PartiallyOrderedSet<T>(
      Arrays.asList(elts).stream().map(Int::new).collect(Collectors.toList()));
}

And after you makeSet you can just directly declare it as Set if the PartiallyOrderedSet is a subclass of Set

Set a = makeSet(new Integer[] {1,2,3,4});
albertjtan
  • 171
  • 2
  • 14