【发布时间】:2017-11-16 16:32:47
【问题描述】:
我一直坚持使用一个 lambda 表达式和使用 Comparator.comparing(...).thenComparing(...) 方法的 Comparator 类来总结两种对流进行排序的方式。
我的两种方法都有效,但是当我将它们放在一起时,根本没有任何效果。
如果您想尝试验证练习,请点击以下链接:
http://codecheck.it/codecheck/files?repo=heigvdcs1&problem=poo3e
这是你必须做的:
对于流中的每个单词,确定“元音”,即元音的数量 - 辅音的数量。生成与元音值配对的元音最高的n 单词。首先按元音排序,然后按字符串排序。完成这个程序。
这一次,您可以使用一个隐藏的静态方法 long Words.vowels(String w),它可以生成 w 中元音的数量,包括重复。
现在我已经做到了:
import java.util.*;
import java.util.stream.*;
public class Streams
{
List<Pair<String, Long>> wordsWithManyVowels(Stream<String> words, int n)
{
return words
.map( w -> Pair.of( w , ( Words.vowels(w) - ( w.length() - Words.vowels(w)))))
.sorted(Comparator.comparingLong(f1 -> -f1.second())
//This part is working without the first comparing
//.thenComparing(f2 -> f2.first().length()))
.limit(n)
.collect(Collectors.toList());
}
}
Pair 类:
import java.util.Objects;
public class Pair<T, S>
{
private T first;
private S second;
public Pair(T firstElement, S secondElement)
{
first = firstElement;
second = secondElement;
}
/*
Use Pair.of(x, y) instead of new Pair<...,...>(x, y)
so you get the type inferred
*/
public static <T, S> Pair<T, S> of(T firstElement, S secondElement)
{
return new Pair<T, S>(firstElement, secondElement);
}
public T first() { return first; }
public S second() { return second; }
public String toString() { return "(" + first + "," + second + ")"; }
public boolean equals(Object otherObject)
{
if (this == otherObject)
return true;
if (otherObject == null || !(otherObject instanceof Pair))
return false;
@SuppressWarnings("unchecked") Pair<T, S> other = (Pair<T, S>) otherObject;
return Objects.equals(first, other.first) &&
Objects.equals(second, other.second);
}
}
【问题讨论】:
-
“什么都没有工作”是什么意思?发生什么了?异常、编译错误、错误的排序顺序?
-
您的
f2 -> f2.first().length())函数是一个 int 提取器,因此该方法不应该是:thenComparingInt吗? -
@MalteHartwig 这意味着该方法无法协同工作。虽然是分开工作
-
附注:不要使用否定来颠倒顺序。在
Long.MIN_VALUE的情况下,由于溢出而不起作用,同样适用于其他整数类型的最小值。很容易说这对于特定属性永远不会发生,但是一旦这个假设不再成立并且你不记得你在代码中的某个地方做出了这个假设,这将花费你很多时间。你可以使用Comparator.comparingLong(Pair<String, Long>::second) .reversed() .thenComparingInt(p -> p.first().length())
标签: java lambda java-8 java-stream comparator