我有一个使用Java 7编写的旧应用程序.它在Java 8 JRE中运行良好.我不打算重写任何代码来使用Java 8功能.将已编译的代码升级到最新的Java 8 JDK是否有任何技术优势?
为了清楚起见,代码目前使用Java 7编译,并且已经使用最新的Java 8 JRE运行.它应该已经从Java 8运行时改进中受益.这个问题是通过编译版本8和运行Java 8编译的字节代码是否可以获得任何好处.
此外,我不关心开发人员生产力等非技术性好处.我认为这些很重要但不是这个问题的重点.我要求没有开发团队的生产代码.它纯粹处于维护模式.
我是Java 8的新手.我仍然不深入了解API,但我已经做了一个小的非正式基准测试来比较新Streams API与优秀旧Collections的性能.
测试包括过滤一个列表Integer,并为每个偶数计算平方根并将其存储在结果List中Double.
这是代码:
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 = System.nanoTime();
long elapsed = 0;
for (Integer i : sourceList) {
if(i % 2 == 0){
result.add(Math.sqrt(i));
} …Run Code Online (Sandbox Code Playgroud) 我刚刚开始研究Java 8并试用lambdas我以为我会尝试重写一个我最近编写的非常简单的东西.我需要将String of Map转换为Column到另一个String to Column的Map,其中新Map中的Column是第一个Map中Column的防御副本.列具有复制构造函数.我到目前为止最接近的是:
Map<String, Column> newColumnMap= new HashMap<>();
originalColumnMap.entrySet().stream().forEach(x -> newColumnMap.put(x.getKey(), new Column(x.getValue())));
Run Code Online (Sandbox Code Playgroud)
但我相信必须有一个更好的方法去做,我会感激一些建议.
JDK中是否有标准的功能接口,什么都不带,什么都不返回?我找不到一个.类似于以下内容:
@FunctionalInterface
interface Action {
void execute();
}
Run Code Online (Sandbox Code Playgroud) 我无法完全理解combinerStreams reduce方法中实现的角色.
例如,以下代码不编译:
int length = asList("str1", "str2").stream()
.reduce(0, (accumulatedInt, str) -> accumulatedInt + str.length());
Run Code Online (Sandbox Code Playgroud)
编译错误说:( 参数不匹配; int无法转换为java.lang.String)
但是这段代码确实编译:
int length = asList("str1", "str2").stream()
.reduce(0, (accumulatedInt, str ) -> accumulatedInt + str.length(),
(accumulatedInt, accumulatedInt2) -> accumulatedInt + accumulatedInt2);
Run Code Online (Sandbox Code Playgroud)
我知道组合器方法用于并行流 - 所以在我的例子中它将两个中间累积的int加在一起.
但是我不明白为什么第一个例子在没有组合器的情况下编译或者组合器如何解决字符串到int的转换,因为它只是将两个整数加在一起.
任何人都可以阐明这一点吗?
我有一台计算机,我故意安装JDK.我有另一台带有JRE的计算机,用于测试等.但是,当我在这台计算机上运行java应用程序,然后在另一台计算机上试用它时,它抱怨JDK是必需的.如何检查JDK是否以某种方式安装在我的系统上?注意:有问题的计算机是Mac.
我只是想在Java 8中将日期字符串转换为DateTime对象.运行以下行:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '20140218' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor:
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Run Code Online (Sandbox Code Playgroud)
语法与此处的建议相同,但我遇到了一个例外.我在用JDK-8u25.
Java 9问世,Observer已被弃用.这是为什么?这是否意味着我们不应该再实施观察者模式了?
知道什么是更好的选择会很好吗?
我正在玩Java 8,以了解如何作为一等公民的功能.我有以下代码段:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是使用方法引用displayInt将方法传递给方法myForEach.编译器会产生以下错误:
src/test/Test.java:9: error: cannot find symbol
list.forEach(functionToBlock(myFunction));
^
symbol: method functionToBlock(Function<Integer,Void>)
location: class Test
src/test/Test.java:25: error: method myForEach in class Test cannot be applied …Run Code Online (Sandbox Code Playgroud) 默认方法是我们的Java工具箱中一个不错的新工具.但是,我尝试编写一个定义default该toString方法版本的接口.Java告诉我这是禁止的,因为声明的方法java.lang.Object可能不会被default编辑.为什么会这样?
我知道存在"基类永远胜利"规则,因此默认情况下(pun;),方法的任何default实现Object都会被方法覆盖Object.但是,我认为没有理由说明Object规范中的方法不应该有例外.特别是对于toString具有默认实现可能非常有用.
那么,Java设计者决定不允许default方法覆盖方法的原因是什么Object?
java ×10
java-8 ×10
java-stream ×3
performance ×2
collections ×1
datetime ×1
deprecated ×1
function ×1
interface ×1
java-9 ×1
lambda ×1
macos ×1
map ×1