Java8 data collection counter with more information

I am using java 8 counting collector to get information on the number of values.

For ex; If I have a thread stream 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 can count the occurrences of each word in a document by doing

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

The result is a structure like

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

However, I would like the account to be associated with the docId that it came from. For example, I would like to have a structure like

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

where randId1they randId2can be generated at runtime (I just need to find the path to a unique source), but ()represents Pair from Apache.

I tried to wrap the doc in Pairfrom (docId, doc), but I was stuck in changing the lookupCollectors.counting()

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 to get output in the right format?

+4
3

?

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

Long Pair # collectAndThen:

              //  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());
+3

... Pair AbstractMap.SimpleEntry, , .

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

:

[{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}]
+5

, :

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

Pair.of , , . , Collectors.toMap . , , :

[{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)}]

, , , , :

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
}

:

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 that the private method is declared in the same class that you are building for the external thread. If not, change the method link this::collectInnerDocsaccordingly.

+2
source

Source: https://habr.com/ru/post/1678194/


All Articles