我有需要迭代的列表.我使用的是Java 8中的新foreach.我发现它比旧的不那么优雅的方法慢得多.我已经阅读了这个堆栈讨论,它更多地关注最佳实践,而不是真实世界的实现.我理解,对于lambda来说,还有额外的开销来源于方法传递,但我仍然不理解第一个和第三个案例之间的差异,并且我认为我可能错误地使用它.还没有为lambda实现优化,还是有更好的方法可以使用它们来获得更高的性能?下面是一个示例性示例,而不是我的实际代码.
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Memes> memesList =
Arrays.asList(
new OldMemes(),
new TrendingMemes()
);
//----------------------------Benchmark
long start, end;
start = System.nanoTime();
System.out.format("for each started @%d\n", start);
//Old way.
for(Memes memes : memesList){
memes.showMeme();
}
//-----------------------------Benchmark
end = System.nanoTime();
System.out.println((end - start));
start = System.nanoTime();
System.out.format("for started @%d\n", start);
//Other way
for(Iterator<Memes> i = memesList.iterator(); i.hasNext(); ) {
i.next().showMeme();
}
//-----------------------------Benchmark
end = System.nanoTime(); …Run Code Online (Sandbox Code Playgroud) 在内部迭代仅与所执行的forEach一个的方法Collection
的对象或还与Stream的方法等filter,reduce等?
我在oracle网站上学习过Java.在那,我看到了这样的例子
public class Horse {
public String identifyMyself() {
return "I am a horse.";
}
}
public interface Flyer {
default public String identifyMyself() {
return "I am able to fly.";
}
}
public interface Mythical {
default public String identifyMyself() {
return "I am a mythical creature.";
}
}
public class Pegasus extends Horse implements Flyer, Mythical {
public static void main(String... args) {
Pegasus myApp = new Pegasus();
System.out.println(myApp.identifyMyself());
}
}
Run Code Online (Sandbox Code Playgroud)
我能写这样的界面吗?我希望我只能在界面中编写抽象函数.那么为什么在oracle网站上他们给出了这样的例子呢?
我刚才读到:关于java 8的一切 ,java 8添加了Arrays.parallelSetAll()
int[] array = new int[8];
AtomicInteger i= new AtomicInteger();
Arrays.parallelSetAll(array, operand -> i.incrementAndGet());
Run Code Online (Sandbox Code Playgroud)
[编辑]对于阵列中相同数量的元素,在同一台机器上是O(1)还是恒定时间复杂度?方法名称表示什么样的性能改进?
我正在尝试将项目导出为NetBeans 8中的.jar,我意识到它应该自动保存.我发现的所有资源都指出它应该位于<myProjectFolder>/dist,但该目录不存在 - 是否有必须切换的设置?
我有一个interface操作如下:
interface Operation {
public double calc(double a, double b);
}
Run Code Online (Sandbox Code Playgroud)
我用它来做enum.
public enum Operations {
POWER("^", new Operation(){
public double calc(double a, double b) {
return Math.pow(a, b);
}
}),
MULTIPLICATION("*", new Operation(){
public double calc(double a, double b) {
return a * b;
}
}),
//...
private String op;
private Operation calc;
public Operations(String op, Operation calc) {
this.op = op;
this.calc = calc;
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用lambda表达式缩短我的代码,但我似乎无法正确使用语法.oracle教程对我来说有点太复杂了.有人可以用简单的方式向我解释一下吗?
编辑:我试过:
POWER ("^", (double a, …
我有一台运行Windows XP 32的旧机器.
因为Oracle不再支持WXP 32位,所以我在这里按照了很棒的答案手动安装了jdk:
在Windows XP上安装JDK8 - advapi32.dll错误
但是我还没有将它添加到JAVA_PATH,它被设置为jdk 6.我不知道是否会伤害它也会有害,但我想在这里保守,因为这里有很多程序机器需要jdk 6.我现在不想破坏任何工作.
我想在eclipse中玩弄它来测试新的Java 8功能.我已经在窗口 - >首选项下的"已安装的JRE"部分中手动添加了新的jre 8.在编译器组合中,可用的最高级别是1.7(前一段时间安装了jdk7,这次是使用官方安装程序,但我仍然将java 6保留为系统中的默认值).我没有看到添加新关卡的任何选项.
因此,在使用新JRE创建新项目时,会显示一条警告:
当前工作空间使用1.4 JRE,编译器合规性级别为1.6.建议不要这样做,并且应该更改JRE或编译器合规性级别.
似乎是手动安装JDK导致的问题.
我正在使用Eclipse Kepler v4.3.1,但我也在旧的3.x安装中测试了这个,结果相同.
任何帮助将不胜感激.
为了掌握Java8,我试图实现一个简单的Repo来存储不同对象的列表.
我的出发点是:
public class MyRepo {
Map<Class, List<?>> repo =new HashMap<>();
public List<?> get(Class c){
List<?> result= repo.get(c);
return (result==null)?new ArrayList<>():(List<?>) result;
}
public void put(Class c,List<?> l){
repo.put(c, l);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我能够插入列表并检索列表.
我的下一步是实现一个通用的过滤方法:
public List<?> find(Class c, Predicate<?> predicate){
List<?> result= repo.get(c);
return (result==null)?new ArrayList<>():(List<?>)result.stream().filter(predicate);
}
Run Code Online (Sandbox Code Playgroud)
结果
java: incompatible types: java.util.function.Predicate<capture#1 of ?> cannot be converted to java.util.function.Predicate<? super capture#2 of ?>
Run Code Online (Sandbox Code Playgroud)
如何实现通用的查找方法?
我不得不承认,我不确定,设计与
Map<Class, List<?>> repo =new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
是个不错的选择.也许有更好的解决方案.
有几天我们有JDK8.我试图用SPDY服务器运行Jetty.在JDK8中没有NPN支持,所以它结束于:
[qtp22068985-16-selector-ServerConnectorManager@12e7b0e/1] WARN
org.eclipse.jetty.io.SelectorManager - Exception while notifying connection SslConnection@e1448a{NEED_UNWRAP,eio=-1/-1,di=-1} ->
NPNServerConnection@382d40{IDLE}
java.lang.NoSuchMethodError:
sun.security.ssl.HandshakeHash.<init>(ZZLjava/util/Set;)V
at sun.security.ssl.Handshaker.activate(Handshaker.java:493)
Run Code Online (Sandbox Code Playgroud)
我npn-boot-1.1.6.v20130911.jar在CLASSPATH上运行它,它与最新的JDK7一起使用.
有没有关于如何在JDK8上使用SPDY运行Jetty的解决方法?
我最近在Spring Framework中下载了Spring MVC(Version 4.0.2.RELEASE)模块的源代码.我的目的是针对模块的实际源代码而不是实际.jar文件运行.(长话,仅用于测试目的).
下载源代码后,我jar从项目中删除了文件,编译并部署到服务器.当我点击调度程序servlet处理的其中一个URL时,我遇到了错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0': Invocation of init method failed; nested exception is java.lang.Error: Unresolved compilation problems:
The import java.time cannot be resolved
The method toZoneId() is undefined for the type TimeZone
ZoneId cannot be resolved
Run Code Online (Sandbox Code Playgroud)
经过一些研究和挖掘源代码后,我意识到错误中提到的类是JDK/JRE 1.8的一部分.在删除.jar文件之前,我的项目在JDK/JRE 1.7上运行没有问题.
我的问题是Spring如何包含JDK 1.8中的类,但仍设法在JDK/JRE 1.7下运行?为什么在使用Spring的.jar文件时不会抛出异常,但是当我提供源代码时它被抛出(并且缺少jar文件)?
全栈跟踪
Mar 26, 2014 7:27:18 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet dispatcher
java.lang.Error: Unresolved compilation problems:
The import java.time cannot be …Run Code Online (Sandbox Code Playgroud)