Tag: java 8

为什么这个Java 8程序不能编译?

这个程序在Java 7(或者在带有-source 7 Java 8中)编译得很好,但是没有用Java 8编译: interface Iface<T> {} class Impl implements Iface<Impl> {} class Acceptor<T extends Iface<T>> { public Acceptor(T obj) {} } public class Main { public static void main(String[] args) { Acceptor<?> acceptor = new Acceptor<>(new Impl()); } } 结果: Main.java:10: error: incompatible types: cannot infer type arguments for Acceptor<> Acceptor<?> acceptor […]

如何判断是否安装了JRE或JDK

我有一台计算机,我故意安装JDK。 我有另一台电脑与JRE,除其他外,testing。 但是,当我在这台计算机上运行java应用程序,然后在另一台计算机上尝试时,它抱怨JDK是必需的。 如何检查JDK是否安装在我的系统上? 注意:有问题的计算机是Mac。

在Java 8中,如何使用lambda将Map <K,V>转换为另一个Map <K,V>?

我刚刚开始研究Java 8并尝试使用lambdaexpression式,我想我会尝试重写最近编写的一个非常简单的东西。 我需要将一个string映射到列到另一个string映射到列新地图列是在第一个地图列的防御副本。 列有一个拷贝构造函数。 我到目前为止最接近的是: Map<String, Column> newColumnMap= new HashMap<>(); originalColumnMap.entrySet().stream().forEach(x -> newColumnMap.put(x.getKey(), new Column(x.getValue()))); 但我确定有一个更好的方法来做到这一点,我会很感激的一些build议。

Java 8 lambda无效参数

假设我在Java 8中有以下function接口: interface Action<T, U> { U execute(T t); } 对于某些情况,我需要一个没有参数或返回types的动作。 所以我写这样的东西: Action<Void, Void> a = () -> { System.out.println("Do nothing!"); }; 不过,它给我编译错误,我需要把它写成 Action<Void, Void> a = (Void v) -> { System.out.println("Do nothing!"); return null;}; 这是丑陋的。 有没有什么办法摆脱Voidtypes的参数?

如何在Java8中为void(不是Void)方法指定函数types?

我正在玩Java 8,以了解如何作为一stream的公民function。 我有以下代码片段: package test; import java.util.*; import java.util.function.*; public class Test { public static void myForEach(List<Integer> list, Function<Integer, Void> myFunction) { list.forEach(functionToBlock(myFunction)); } public static void displayInt(Integer i) { System.out.println(i); } public static void main(String[] args) { List<Integer> theList = new ArrayList<>(); theList.add(1); theList.add(2); theList.add(3); theList.add(4); theList.add(5); theList.add(6); myForEach(theList, Test::displayInt); } } 我想要做的是传递方法displayInt方法myForEach使用方法引用。 要编译器产生以下错误: […]

Java 8stream:多个filter与复杂的条件

有时候你想过滤一个Stream并且有多个条件: myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) … 或者你可以做一个复杂的条件和一个单一的 filter : myList.stream().filter(x -> x.size() > 10 && x -> x.isCool()) … 我的猜测是,第二种方法有更好的性能特点,但我不知道 。 第一种方法赢得了可读性,但是什么更好的performance?

使用stream添加BigDecimals

我有一个BigDecimals集合(在这个例子中是一个LinkedList ),我想将它们加在一起。 有没有可能为此使用stream? 我注意到Stream类有几个方法 Stream::mapToInt Stream::mapToDouble Stream::mapToLong 每个方法都有一个方便的sum()方法。 但是,正如我们所知, float和double算术几乎总是一个坏主意。 那么,总结BigDecimals有一个方便的方法吗? 这是迄今为止的代码。 public static void main(String[] args) { LinkedList<BigDecimal> values = new LinkedList<>(); values.add(BigDecimal.valueOf(.1)); values.add(BigDecimal.valueOf(1.1)); values.add(BigDecimal.valueOf(2.1)); values.add(BigDecimal.valueOf(.1)); // Classical Java approach BigDecimal sum = BigDecimal.ZERO; for(BigDecimal value : values) { System.out.println(value); sum = sum.add(value); } System.out.println("Sum = " + sum); // Java 8 approach values.forEach((value) […]

Java 8:Streams vs Collections的性能

我是Java 8的新手。我还是不太了解这个API,但是我做了一个小的非正式的基准testing来比较新的Streams API和旧版本的性能。 testing包括过滤Integer列表,并为每个偶数计算平方根并将其存储在Double的结果List中。 这里是代码: public static void main(String[] args) { //Calculating square root of even numbers from 1 to N int min = 1; int max = 1000000; List<Integer> sourceList = new ArrayList<>(); for (int i = min; i < max; i++) { sourceList.add(i); } List<Double> result = new LinkedList<>(); //Collections approach long t0 […]

为什么这个Java 8 lambda无法编译?

以下Java代码无法编译: @FunctionalInterface private interface BiConsumer<A, B> { void accept(A a, B b); } private static void takeBiConsumer(BiConsumer<String, String> bc) { } public static void main(String[] args) { takeBiConsumer((String s1, String s2) -> new String("hi")); // OK takeBiConsumer((String s1, String s2) -> "hi"); // Error } 编译器报告: Error:(31, 58) java: incompatible types: bad return type in […]

Java 8 lambdas,Function.identity()或t-> t

我有一个关于Function.identity()方法的用法的问题。 想象下面的代码: Arrays.asList("a", "b", "c") .stream() .map(Function.identity()) // <- This, .map(str -> str) // <- is the same as this. .collect(Collectors.toMap( Function.identity(), // <– And this, str -> str)); // <– is the same as this. 是否有任何理由,你应该使用Function.identity()而不是str->str (反之亦然)。 我认为第二个选项更具可读性(当然是品味)。 但是,有什么“真正的”理由为什么应该被优先考虑呢?