Anoop
Anoop

Reputation: 5720

Java8 counting collector with additional information

I'm using counting collector of java 8 to get information about the count of values.

For ex; If I have bunch of streams like

Stream<String> doc1 = Stream.of("a", "b", "c", "b", "c");
Stream<String> doc2 = Stream.of("b", "c", "d");
Stream<Stream<String>> docs = Stream.of(doc1, doc2);

I am able to count the occurrences of each word in a doc by doing

List<Map<String, Long>> collect = docs
    .map(doc -> doc.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())))
    .collect(Collectors.toList());

This results in a structure as

[
{a=1, b=2, c=2}, 
{b=1, c=1, d=1}
]

However, I would like to have the count be associated with the docId from which it originated from. For example I would like to have a structure as

[
{a=(randId1, 1), b=(randId1, 2), c=(randId1, 2)}, 
{b=(randId2, 1), c=(randId2, 1), d=(randId2, 1)}
]

where randId1 and randId2 can be generated at runtime(I just need a way to trace back to a unique source) and () represents a Pair class from Apache.

I have tried to wrap the doc in a Pair of (docId, doc) but I am stuck at modifying the Collectors.counting() substitution

List<Map<String, Long>> collect = docs.map(doc -> Pair.of(UUID.randomUUID(), doc))
    .map(p -> p.getRight().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())))
    .collect(Collectors.toList());

How do I get the output in the format needed ?

Upvotes: 4

Views: 967

Answers (3)

holi-java
holi-java

Reputation: 30696

How about this?

List<Map<String, Pair<UUID, Long>>> collect = docs.map(doc -> {
    UUID id = UUID.randomUUID();
    return doc.collect(groupingBy(
        identity(),
    //  v--- adapting Collector<?,?,Long> to Collector<?,?,Pair>    
        collectingAndThen(counting(), n -> Pair.of(id, n))
    ));
}).collect(Collectors.toList());

I'm just copy your code snippet and adapting your last generic argument Long to Pair by Collectors#collectingAndThen:

              //  v--- the code need to edit is here
List<Map<String, Long>> collect = docs
.map(doc -> doc.collect(Collectors.groupingBy(Function.identity()
 //                    the code need to edit is here ---v
                                             ,Collectors.counting())))
.collect(Collectors.toList());

Upvotes: 4

fps
fps

Reputation: 34470

I think you could do it as follows:

List<Map<String, Pair<UUID, Long>>> result = docs
    .map(doc -> Pair.of(UUID.randomUUID(), doc))
    .map(p -> p.getRight() // right: doc stream
        .map(word -> Pair.of(word, p.getLeft()))) // left: uuid
    .map(stream -> stream.collect(Collectors.toMap(
        Pair::getLeft, // word
        p -> Pair.of(p.getRight(), 1L), // right: uuid
        (p1, p2) -> Pair.of(p1.getLeft(), p1.getRight() + p2.getRight())))) // merge
    .collect(Collectors.toList());

I've use Pair.of multiple times to pass around both the word and the random doc id. Finally, I've used Collectors.toMap with a function to merge values when there's a collision on the keys. The result is exactly as you want, i.e.:

[{a=(fa843dec-3e02-4811-b34f-79949340b4c5,1), 
  b=(fa843dec-3e02-4811-b34f-79949340b4c5,2), 
  c=(fa843dec-3e02-4811-b34f-79949340b4c5,2)}, 
 {b=(dc2ad8c7-298a-433e-8b27-88bd3c8eaebb,1), 
  c=(dc2ad8c7-298a-433e-8b27-88bd3c8eaebb,1), 
  d=(dc2ad8c7-298a-433e-8b27-88bd3c8eaebb,1)}]

Maybe this could be improved by moving the code that collects the inner streams to a helper method:

private Map<String, Pair<UUID, Long>> collectInnerDoc(
        Stream<Pair<String, UUID>> stream) {
    return stream.collect(Collectors.toMap(
        Pair::getLeft, // word
        p -> Pair.of(p.getRight(), 1L), // random doc id
        (p1, p2) -> Pair.of(p1.getLeft(), p1.getRight() + p2.getRight()))); // merge
}

You could then use this method to collect your outer stream:

List<Map<String, Pair<UUID, Long>>> result = docs
    .map(doc -> Pair.of(UUID.randomUUID(), doc))
    .map(p -> p.getRight() // right: doc stream
        .map(word -> Pair.of(word, p.getLeft()))) // left: uuid
    .map(this::collectInnerDoc) // map inner stream to map
    .collect(Collectors.toList());

This assumes the private method is declared in the same class you are collecting the outer stream. If this is not the case, change the this::collectInnerDocs method reference accordingly.

Upvotes: 2

Eugene
Eugene

Reputation: 121048

This ain't very readable... I've replaced Pair with AbstractMap.SimpleEntry since it does the same thing and I already have it on my classpath.

 List<Map<String, AbstractMap.SimpleEntry<Long, UUID>>> result = docs.map(doc -> doc.collect(Collectors.collectingAndThen(
            Collectors.groupingBy(Function.identity(), Collectors.counting()),
            map -> {
                UUID rand = UUID.randomUUID();
                return map.entrySet().stream().collect(Collectors.toMap(
                        Entry::getKey,
                        e -> new AbstractMap.SimpleEntry<>(e.getValue(), rand)));
            })))
            .collect(Collectors.toList());

    System.out.println(result);

And the output of this:

[{a=1=890d7276-efb7-41cc-bda7-f2dd2859e740, 
  b=2=890d7276-efb7-41cc-bda7-f2dd2859e740, 
  c=2=890d7276-efb7-41cc-bda7-f2dd2859e740}, 

 {b=1=888d78a5-0dea-4cb2-8686-c06c784d4c66, 
  c=1=888d78a5-0dea-4cb2-8686-c06c784d4c66, 
  d=1=888d78a5-0dea-4cb2-8686-c06c784d4c66}]

Upvotes: 5

Related Questions