1

possible replica of Java int[] array to HashSet<Integer> but badly answered so new question.

I have a set that I'm trying to declare :

int[] flattened = Arrays.stream(arcs).flatMapToInt(Arrays::stream).toArray();
Set<Integer> set = new HashSet<Integer>(Arrays.asList(flattened));

but as the return type of Arrays.asList is a list itself, it cannot resolve. What would be the best method to turn a list of int[] into Set<Integer>

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
entercaspa
  • 674
  • 2
  • 7
  • 19
  • If you just changed your array to `Integer[] flattened` it would work fine. Your problem is with types. `HashSet` does take a `Collection` (and thus a `List`) as its constructor argument just fine. – jbx Oct 26 '18 at 13:48
  • By the way, if the integers in your set can only ever have small non-negative values (e.g. if all your values fall in the range 0..1024) then consider using `BitSet` instead of `Set`. – DodgyCodeException Oct 26 '18 at 13:59
  • I would like to change the title to be **List to HashSet**, you already said in your question *What would be the best method to turn a list of int[] into Set*!! – Youcef LAIDANI Oct 26 '18 at 14:06

2 Answers2

5

..What would be the best method to turn a list of int[] into Set

In this case you can use :

List<int[]> arcs = ...;
Set<Integer> set = arcs.stream()
        .flatMapToInt(Arrays::stream)
        .boxed()
        .collect(Collectors.toSet());

Example :

List<int[]> arcs = new ArrayList<>(Arrays.asList(new int[]{1, 2, 3}, new int[]{3, 4, 6}));

Outputs

[1, 2, 3, 4, 6]

Note : as Jack mention, to guaranteed the collection to be HashSet you can collect like so :

...
.collect(Collectors.toCollection(() -> new HashSet<>()));
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
4

You should be able to do it as a one-liner, like so:

Set<Integer> set = Arrays.stream(arcs).flatMapToInt(Arrays::stream).collect(Collectors.toSet());

Updated: Jack comments below that Collectors.toSet() is not guaranteed to return a HashSet -- in practice I think it usually does, but no guarantees -- so it would be better to use:

Set<Integer> set = Arrays.stream(arcs).flatMapToInt(Arrays::stream)
  .collect(Collectors.toCollection(() -> new HashSet<>()));

And as DodgyCodeException points, out, OP's sample had an additional problem I wasn't addressing, so tweak using this:

Set<Integer> set = Arrays.stream(arcs)
    .flatMapToInt(Arrays::stream)
    .boxed() // <-- converts from IntStream to Stream<Integer>
    .collect(Collectors.toCollection(() -> new HashSet<>()));
jwismar
  • 12,164
  • 3
  • 32
  • 44
  • This is not guaranteed to be an `HashSet` as per OP requirement, probably `.toCollection(() -> new HashSet<>())` is better – Jack Oct 26 '18 at 13:33
  • Fair enough. The implementation I'm using happens to use HashSet... I can add an elaboration to that effect. – jwismar Oct 26 '18 at 13:33
  • @Jack Depends on what you consider OP requirements. The question contains `Set = …`, which I interpret as meaning a Set of Integer is required. – VGR Oct 26 '18 at 13:37
  • 1
    Won't work as `IntStream.collect` takes 3 arguments. – DodgyCodeException Oct 26 '18 at 13:37
  • @VGR: well, the title of the question is `int[]` to `HashSet`, there's not much left to interpretation. – Jack Oct 26 '18 at 13:38
  • @DodgyCodeException I was assuming that OP's original code was mostly working... I don't know what `arcs` is, for example, so I'm not attempting to run. But this should get him closer. – jwismar Oct 26 '18 at 13:40
  • 1
    Whatever `arcs` is, the call to `flatMapToInt` definitely produces an `IntStream` and not a `Stream`, so from there on, you know exactly what the type is. – DodgyCodeException Oct 26 '18 at 13:41
  • so arcs is a int[][], and with collect(collectors.toC.....) i get a collect() in Instream cannot be applied to parameters – entercaspa Oct 26 '18 at 13:42
  • 1
    @entercaspa you need to either supply three parameters (as per the Javadoc) or call `.boxed()` before `.collect(...)`. – DodgyCodeException Oct 26 '18 at 13:43
  • one second i must quickly look up what boxed does – entercaspa Oct 26 '18 at 13:43
  • marked as answer because i understood more of the methods due to discussion. thank you all for your help – entercaspa Oct 26 '18 at 13:48
  • @Holger I hope readers of your comment spot your typo ;-) (you meant HashSet, of course, and not HashMap) – DodgyCodeException Oct 26 '18 at 15:21
  • 2
    Instead of `() -> new HashSet<>()` you can simply write `HashSet::new`. – Holger Oct 26 '18 at 15:23
  • 2
    @DodgyCodeException of course. I decided to comment again instead of letting it that wrong – Holger Oct 26 '18 at 15:25