I have a Stream<T>
, is it possible to generate a Map<T, Long>
that increases count by one for every element? I'd also like if it could reverse the T, Long
to Long, T
, before storing it in a Map
, example input/output:
Example contents of Stream<String>
:
kgj
def
dgh
Wanted output for first question:
(kgj, 1)
(def, 2)
(dgh, 3)
Wanted output in the end:
(1, kgj)
(2, def)
(3, dgh)
What I have so far, for a Stream<VertexData>
:
primitives.stream()
.flatMap(primitive -> primitive.getVertexData().stream())
.distinct()
.collect(Collectors.groupingBy(i -> i, Collectors.counting()))
.forEach((k, v) -> System.out.println("k = " + k + " / v = " + v));
This currently turns a Stream<Primitive>
into a Stream<VertexData>
with distinct elements, and then to a Map<VertexData, Long>
that counts occurences, which is not what I want, as I want it to keep on counting at every element that passes.
Is there a way to do what I ask?