假设我们有一个如下列表.CoreResult有一个类型的领域List<Double>.
final List<CoreResult> list = new LinkedList<>(SOME_DATA);
Run Code Online (Sandbox Code Playgroud)
目标是在从每个CoreResult对象中提取特定字段后展平列表.这里有3种可能的选择.他们中的任何一个比其他人更好吗?
选项1:通过map()收集器提取字段并展平收集器内部
final List<Double> A = list.stream().map(CoreResult::getField)
.collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
Run Code Online (Sandbox Code Playgroud)
选项2:map()通过flatMap()简单的收集器提取字段
final List<Double> B = list.stream().map(CoreResult::getField)
.flatMap(Collection::stream).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
选项3:通过flatMap()简单的收集器提取一个字段并平整
final List<Double> C = list.stream().flatMap(
x -> x.getField().stream()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果不需要从CoreResult中提取任何字段,那么答案是否会有所不同,而是想要简单地将其展平List<List<Double>>?
我们如何改变JavaFX画布的背景?我现在唯一的解决方案是:
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLUE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
Run Code Online (Sandbox Code Playgroud)
除了绘制矩形之外还有其他解决方案吗?我在CSS中搜索但是画布没有-fx-background-color
请考虑以下代码段:
public class JavaApplication4 {
static <T> List<T> functionConcat(List<T> l1, List<T> l2) {
return Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());
}
// functionConcat in lambda form
static final BinaryOperator<List<? extends Number>> lambdaConcat = (l1, l2)
-> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());
public static void main(String[] args) {
// DOES NOT WORK with lambdaConcat
final List<Integer> x = new LinkedList<List<Integer>>()
.stream().reduce(new LinkedList<>(), lambdaConcat);
final List<Double> y = new LinkedList<List<Double>>()
.stream().reduce(new LinkedList<>(), lambdaConcat);
// WORKS with functionConcat
final List<Integer> x2 = new LinkedList<List<Integer>>()
.stream().reduce(new LinkedList<>(), JavaApplication4::functionConcat);
final List<Double> y2 …Run Code Online (Sandbox Code Playgroud) 在以下代码段中:
public class IDMapBase<T> extends HashMap<DeviceID, T> {
public Map<DeviceID, T> filterMap(Set<DeviceID> neededIDs) {
return entrySet().stream()
.filter(e -> neededIDs.contains(e.getKey()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么简单地说Entry::getXX而不是Entry<DeviceID, T>>::getXX?我认为这(Map.)Entry将默认为Entry<Object, Object>,它不能用作a的条目Map<DeviceID, T>.