基于列表的最后一项,从嵌套列表中提取有序(根据值)集,Java

时间:2019-05-18 13:48:06

标签: list lambda java-8 set java-stream

我提出了这个问题List of Only One Single value of Filtered List Value

public class Competitor {
  private final int type;
  private final String name;
  private final int power;

  public Competitor(int type, String name, int power) {
    this.type = type;
    this.name = name;
    this.power = power;
  }

  public int getType() {
    return type;
  }

  public String getName() {
    return name;
  }

  public int getPower() {
    return power;
  }

  @Override
  public String toString() {
    return "Competitor{" + "type=" + type + ", name=" + name + ", power=" + power + "} ";
  }

我有一个嵌套列表

List<List<Competitor>> nestedCompetitor = new ArrayList<>(); 

List<Competitor> listCompetitors0 = new ArrayList<>();

listCompetitors0.add(new Competitor(1, "Cat 00", 93));
listCompetitors0.add(new Competitor(1, "Cat 10", 11));
listCompetitors0.add(new Competitor(1, "Cat 23", 20));

List<Competitor> listCompetitors1 = new ArrayList<>();
listCompetitors1.add(new Competitor(2, "Dog 61", 54));
listCompetitors1.add(new Competitor(2, "Dog 18", 40));
listCompetitors1.add(new Competitor(2, "Dog 45", 71));
listCompetitors1.add(new Competitor(2, "Dog 30", 68));

List<Competitor> listCompetitors2 = new ArrayList<>();
listCompetitors2.add(new Competitor(3, "Pig 90", 90));
listCompetitors2.add(new Competitor(3, "Pig 78", 20));

List<Competitor> listCompetitors3 = new ArrayList<>();
listCompetitors3.add(new Competitor(4, "Cow 99", 90));

nestedCompetitor.add(listCompetitors0);
nestedCompetitor.add(listCompetitors1);
nestedCompetitor.add(listCompetitors2);
nestedCompetitor.add(listCompetitors3);

现在,我需要提取一个集合,并提取nestedCompetitor

中包含的每个列表的最后一项
List<Competitor> listLastCompetitor = new ArrayList<>();
nestedCompetitor.stream()
    .forEachOrdered(listCompetitor -> {
      Competitor lastCompetitor = listCompetitor.stream().reduce((first, second) -> second).orElse(null);
      listLastCompetitor.add(lastCompetitor);
    });
List<Integer> listLastPower = new ArrayList<>(listLastCompetitor.stream()
    .map(Competitor::getPower)
    .collect(Collectors.toSet()));

编辑

Set<Integer> setLastSampleRateFinal = nestedNodeResample.stream()
    .map(l -> l.get(l.size() - 1).getSampleRateTarget()) 
    .collect(Collectors.toSet()).stream().sorted().collect(Collectors.toSet());

//我添加了此.stream().sorted().collect(Collectors.toSet())不起作用

List<Integer> list = new ArrayList<>(setLastSampleRateFinal);
Collections.sort(list, Collections.reverseOrder());
Set<Integer>  orderedSetLastSampleRateFinal = new LinkedHashSet<>(list);

如何优化以前的代码(如果可能)?

要检查输出

System.out.println("\nlistLastPower:");
listLastPower.stream().forEach(System.out::print);

1 个答案:

答案 0 :(得分:3)

您可以一次完成,而不要实现中间结果。看起来就是这样。

Set<Integer> listLastPower = nestedCompetitor.stream()
    .map(l -> l.get(l.size() - 1).getPower())
    .collect(Collectors.toSet());

如果要按降序排序,则必须使用内置的Comparator,如下所示。请注意,使用LinkedHashSet构造函数参考时要遵守相遇顺序。

Set<Integer> listLastPower = nestedCompetitor.stream()
    .map(l -> l.get(l.size() - 1).getPower())
    .sorted(Comparator.reverseOrder())
    .collect(Collectors.toCollection(LinkedHashSet::new));