我正在使用一个类来执行我的问题...
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 + "} ";
}
}
现在,我在上课之前创建了一个nestedList
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);
我订购了Set
分类的Power
Set<Integer> setLastPower = nestedCompetitor.stream()
.map(l -> l.get(l.size() - 1).getPower())
.sorted(Comparator.reverseOrder())
.collect(Collectors.toCollection(LinkedHashSet::new));
List<Integer> account = new ArrayList<>();
Long iterations = 1L;
for (Integer powerSet: setLastPower) {
Integer powerCount =
(int)nestedCompetitor.stream()
.map(l -> l.get(l.size() - 1).getPower())
.filter(powerList -> powerList.equals(powerSet)).count();
account.add(powerCount);
System.out.println("powerSet:" + powerSet + " -> " + "number items:" + powerCount);
iterations *=(long)powerCount;
}
System.out.println("iterations:" + iterations);
powerCount
是个项目。
现在,我想根据每个List
项从Set
获得编号项。
我知道我可以使用创建一个过滤器,该过滤器取自Set项,并应用于List并进行计数。
但是,是否有优化的选项?
答案 0 :(得分:1)
您可以使用
Map<Integer,Long> map = nestedCompetitor.stream()
.collect(Collectors.toMap(
l -> l.get(l.size() - 1).getPower(), list -> 1L, Long::sum,
() -> new TreeMap<>(Comparator.<Integer>reverseOrder())));
map.forEach((powerSet,powerCount) ->
System.out.println("powerSet:" + powerSet + " -> " + "powerCount:" + powerCount));
Long iterations = map.values().stream().reduce(1L, Math::multiplyExact);
System.out.println("iterations:" + iterations);
或者
Map<Integer,Long> map = nestedCompetitor.stream()
.collect(Collectors.groupingBy(l -> l.get(l.size() - 1).getPower(),
() -> new TreeMap<>(Comparator.<Integer>reverseOrder()),
Collectors.counting()));
map.forEach((powerSet,powerCount) ->
System.out.println("powerSet:" + powerSet + " -> " + "powerCount:" + powerCount));
Long iterations = map.values().stream().reduce(1L, Math::multiplyExact);
System.out.println("iterations:" + iterations);
这两者的共同点是正确计算收集操作中的出现次数。通常,您会发现toMap
或groupingBy
这两个收集器中的一个更适合于特定任务,但是在这里,差别不大。
原则上,toMap
在有很多不同的键且每个键的出现次数较少的情况下会更有效,而groupingBy
在键的数目较少时会领先一步。不同的键,每个键都有很多元素(不过,对于counting()
,这仅适用于Java 9或更高版本)。