java - 使用 JavaStreams 过滤复杂列表元素 - 代码优化

标签 java optimization java-8 java-stream

我有复杂的元素列表。每个内部列表包含 3 个字段:代码、名称、版本。 我需要获取每个代码的最大版本号的最终列表。

下面的代码片段按预期工作,但正在寻找有效的解决方案。

List<List<Object>> lists = new ArrayList<>();
List<List<Object>> finalList = new ArrayList<>();

// each list contains => code,   name,   version
lists.add(Arrays.asList("code1", "Name1", 1));
lists.add(Arrays.asList("code1", "Name1", 2));
lists.add(Arrays.asList("code1", "Name1", 3));
lists.add(Arrays.asList("code1", "Name1", 3));
lists.add(Arrays.asList("code2", "Name2", 1));
lists.add(Arrays.asList("code2", "Name2", 2));

// get all uniqueCodes
List<Object> uniqueCodes = lists.stream().map(row -> row.get(0)).distinct().collect(Collectors.toList());

// iterate over uniqueCodes 
uniqueCodes.stream().forEach(code -> {
    // get lists for this uniqueCode
    Supplier<Stream<List<Object>>> sameCodeLists = () -> lists.stream().filter(row -> row.get(0) == code);

    // get max version of this uniqueCode
    int maxVersion = sameCodeLists.get().mapToInt(row -> (int) row.get(2)).max().getAsInt();

    // for this uniqueCode, get all Lists containing Max Version
    finalList.addAll(sameCodeLists.get().filter(row -> (int) row.get(2) == maxVersion && row.get(0) == code).collect(Collectors.toList()));
});
System.out.println(finalList);
Input: [[code1, Name1, 1], [code1, Name1, 2], [code1, Name1, 3], [code1, Name1, 3], [code2, Name2, 1], [code2, Name2, 2]]

Output: [[code1, Name1, 3], [code1, Name1, 3], [code2, Name2, 2]]

这是适当的并且符合预期。寻找代码优化以更少的迭代完成工作。

最佳答案

你可以通过以下方式大大简化它

  • 创建一个类并使用其实例而不是 List<Object> s;
  • 雇用Collectors.groupingByCollectors.maxBy .
<小时/>
@Getter
@ToString
@RequiredArgsConstructor
public final class Entity {

  private final String code;
  private final String name;
  private final int version;

  public static void main(String[] args) {
    final List<Entity> entities = Arrays.asList(
      new Entity("code1", "Name1", 1),
      new Entity("code1", "Name1", 2),
      new Entity("code1", "Name1", 3),
      new Entity("code1", "Name1", 3),
      new Entity("code2", "Name2", 1),
      new Entity("code2", "Name2", 2)
    );

    final Map<String, Optional<Entity>> result = entities.stream()
      .collect(Collectors.groupingBy(Entity::getCode,
        Collectors.maxBy(Comparator.comparingInt(Entity::getVersion))));

    System.out.println(result);
    // {
    //  code2=Optional[Entity(code=code2, name=Name2, version=2)], 
    //  code1=Optional[Entity(code=code1, name=Name1, version=3)]
    // }
  }

}

正如@Aomine 在评论中建议的那样,Collectors.collectingAndThen可以使用整理器函数将其映射到最终结果。

Map<String, Optional<Integer>> result = entities.stream()
  .collect(groupingBy(Entity::getCode,
    collectingAndThen(maxBy(Comparator.comparingInt(Entity::getVersion)),
      opEntity -> opEntity.map(Entity::getVersion))));

System.out.println(result);
// {code2=Optional[2], code1=Optional[3]}

更新

这不是一项简单的任务,因此 Stream API 可能不太适合。尽管如此,我想出了以下似乎可以完成这项工作的方法。

List<Entity> result = entities.stream()
    // group by code and sort the resulting lists by version
    .collect(groupingBy(Entity::getCode, HashMap::new,
            collectingAndThen(toList(), e -> e.stream().sorted(comparingInt(Entity::getVersion).reversed()).collect(toList()))))
    .values()
    .stream()
    // keep only the max values
    .map(l -> l.size() > 0 ? l.stream().filter(i -> i.getVersion() == l.get(0).getVersion()).collect(toList()) : l)
    .flatMap(List::stream)
    .collect(toList());

System.out.println(result);
// [
//  Entity(code=code2, name=Name2, version=2), 
//  Entity(code=code1, name=Name1, version=3), 
//  Entity(code=code1, name=Name1, version=3)
// ]

关于java - 使用 JavaStreams 过滤复杂列表元素 - 代码优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57837610/

相关文章:

java - Eclipse 看不到来自目标/生成源的类

c - 如何强制 GAS 生成与输入相同的输出?

mysql - 在 MySQL 中,如何连接两个在 WHERE 条件下都有列的非常大的表?

c++ - 我怎样才能优化这个C++?

Java 8 : how to get the first number greater than 10 in a stream?

java - 排序行为不同?

java - arraylist 的质量变化值

java - 格式化输出,保留三位小数

java - 使用迭代器与收集完成流?

java - 如何从 Tomcat url 中删除端口号?