Stream - collect by property and max.

Problem Statement

Given the following class (simplified for the question):

public static class Match {

  private final String type;
  private final int score;

  public Match(String type, int score) {
    this.type = type;
    this.score = score;
  } 

  public String getType() {
    return type;
  }

  public int getScore() {
    return score;
  }
}

I have Stream<Match>one that contains several instances of the class, the same type appears several times, but with different values:

Stream.of(new Match("A", 1), new Match("A", 2), new Match("A", 4), new Match("A", 10),
          new Match("B", 3), new Match("B", 6), new Match("B", 12),
          new Match("C", 1));

Now I want to collect the stream so that the result List<Match>is containing only instances with the highest score of each type.

What i tried

The following code works, but I'm not sure if this is an “optimal” solution (other than terrible reading and formatting):

.collect(Collectors.collectingAndThen(
          Collectors.groupingBy(Match::getType, Collectors.collectingAndThen(
              Collectors.toList(),
              l -> l.stream().max(Comparator.comparing(Match::getScore)).get())), Map::values))
      .forEach(m -> System.out.println(m.getType() + ": " + m.getScore()));

and

.collect(Collectors.collectingAndThen(
          Collectors.groupingBy(Match::getType, Collectors.maxBy(Comparator.comparing(Match::getScore))), Map::values))
      .forEach(m -> m.ifPresent(ma -> System.out.println(ma.getType() + ": " + ma.getScore())));

Output (correct):

A: 10,
B: 12,
C: 1


Also, I was not able to retrieve the universal static method returning the collector so that I could just use it where I need it, for example:
.collect(distinctMaxByProperty(Match::getType, Match::getScore)

Any help would be greatly appreciated!

+4
3

List, , ,

Map<String,Match> result =
    Stream.of(new Match("A", 1), new Match("A", 2), new Match("A", 4), new Match("A", 10),
              new Match("B", 3), new Match("B", 6), new Match("B", 12), new Match("C", 1))
        .collect(Collectors.groupingBy(Match::getType, Collectors.collectingAndThen(
            Collectors.reducing(BinaryOperator.maxBy(
                                    Comparator.comparingInt(Match::getScore))),
            Optional::get)));

, Optional groupingBy, , toMap` :

Map<String,Match> result =
    Stream.of(new Match("A", 1), new Match("A", 2), new Match("A", 4), new Match("A", 10),
              new Match("B", 3), new Match("B", 6), new Match("B", 12), new Match("C", 1))
        .collect(Collectors.toMap(Match::getType, Function.identity(),
                 BinaryOperator.maxBy(Comparator.comparingInt(Match::getScore))));

Map,

result.values().forEach(m -> System.out.println(m.getType() + ": " + m.getScore()));

Match, :

Stream.of(new Match("A", 1), new Match("A", 2), new Match("A", 4), new Match("A", 10),
          new Match("B", 3), new Match("B", 6), new Match("B", 12), new Match("C", 1))
    .collect(Collectors.toMap(Match::getType, Match::getScore, Math::max))
    .forEach((type,score) -> System.out.println(type + ": " + score));
+4

.

: Collectors.toMap()

Collectors.toMap() .

stream.collect(Collectors.toMap(Match::getType, Match::getScore, Math::max));

List<Match>(),

stream
    .collect(Collectors.toMap(Match::getType, Match::getScore, Math::max))
    .entrySet()
    .stream()
    .map(e -> new Match(e.getKey(), e.getValue()))
    .collect(Collectors.toList());

:

, . :

public class MaxMatch implements Collector<Match, Map<String, Integer>, List<Match>> {
    @Override
    public Supplier<Map<String, Integer>> supplier() {
        return HashMap::new;
    }

    @Override
    public BiConsumer<Map<String, Integer>, Match> accumulator() {
        return (map, match) -> {
            Integer score = match.getScore();
            if(map.containsKey(match.getType())) {
                score = Math.max(score, map.get(match.getType()));
            }
            map.put(match.getType(), score);
        };
    }

    @Override
    public BinaryOperator<Map<String, Integer>> combiner() {
        return (mapA, mapB) -> {
            mapA.forEach((k, v) -> {
                if(mapB.containsKey(k)) { mapB.put(k, Math.max(v, mapB.get(k))); }
                else { mapB.put(k, v); }
            });
            return mapB;
        };
    }

    @Override
    public Function<Map<String, Integer>, List<Match>> finisher() {
        return (map) -> map.entrySet().stream().map(e -> new Match(e.getKey(), e.getValue())).collect(Collectors.toList());
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.emptySet();
    }
}

:

stream.collect(new MaxMatch());

, :)

+3

Try using TreeMapfor this:

UPDATE

List<Match> matchStream1 = matchStream.
            collect(Collectors.groupingBy(Match::getType,
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Match::getScore)))))
            .values()
            .stream()
            .map(TreeSet::last)
            .collect(Collectors.toList());

In case your class Matchimplements Comparable. You can simplify this:

() -> new TreeSet<>(Comparator.comparing(Match::getScore))

:

TreeSet::new
+1
source

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


All Articles