考虑以下几乎可编译的Java 8代码:
public static void main(String[] args) {
LinkedList<User> users = null;
users.add(new User(1, "User1"));
users.add(new User(2, "User2"));
users.add(new User(3, "User3"));
User user = users.stream().filter((user) -> user.getId() == 1).findAny().get();
}
static class User {
int id;
String username;
public User() {
}
public User(int id, String username) {
this.id = id;
this.username = username;
}
public void setUsername(String username) {
this.username = username;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return …Run Code Online (Sandbox Code Playgroud) 我有以下Java6和Java8代码:
List<ObjectType1> lst1 = // a list of ObjectType1 objects
List<ObjectType2> lst2 = // a list of ObjectType1 objects, same size of lst1
List<ObjectType3> lst3 = new ArrayLis<ObjectType3>(lst1.size());
for(int i=0; i < lst1.size(); i++){
lst3.add(new ObjectType3(lst1.get(i).getAVal(), lst2.get(i).getAnotherVal()));
}
Run Code Online (Sandbox Code Playgroud)
Java8中是否有任何方法可以使用Lambda以更简洁的方式处理上一个?
我设计了一个javkx应用程序在jdk 7中工作正常.当我尝试在java 8中运行它时,我得到以下异常:
javafx.fxml.LoadException:
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3191)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3164)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3140)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3132)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:364)
at javafx.scene.Scene.<init>(Scene.java:232)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.concurrent.EventHelper.fireEvent(EventHelper.java:219)
at javafx.concurrent.Task.fireEvent(Task.java:1357)
at javafx.concurrent.Task.setState(Task.java:720)
at javafx.concurrent.Task$TaskCallable$2.run(Task.java:1438)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)
Run Code Online (Sandbox Code Playgroud)
我发现原因是在控制器类的initialize方法中我无法在任何静态组件中使用内置方法.(例如:staticMyTextField.setText()导致java …
在Java 8中有"方法参考"功能.其中一种是"引用特定类型的任意对象的实例方法"
http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html#type
有人可以解释"特定类型的任意对象"在这种情况下的含义吗?
在Java 8之前,我们无法在本地类中使用非final变量.但是现在他们允许最终以及有效的决赛(谁的价值观没有改变),可以由当地的班级推荐.我所知道的(如果我错了,请纠正我),他们不支持引用非最终值,因为可以更改值.那么,他们现在如何支持它以及之前为什么不支持它.
在Java 8中,Stream有一个方法减少:
T reduce(T identity, BinaryOperator<T> accumulator);
Run Code Online (Sandbox Code Playgroud)
累加器运算符是否允许修改其任一参数?我认为不是因为JavaDoc说累加器应该是NonInterfering,尽管所有的例子都谈到修改集合,而不是修改集合的元素.
所以,举一个具体的例子,如果我们有
integers.reduce(0, Integer::sum);
Run Code Online (Sandbox Code Playgroud)
并假设一个Integer可变的时刻,sum可以通过向其添加(就地)第二个参数的值来修改其第一个参数吗?
我认为不是,但我也想要一个这个干扰导致问题的例子.
我有两个与这些类似的功能:
public static <In extends Number, Out extends Number> Out test(In in, Function<In, Out> f) {
Out x = f.apply(in);
return test(in, x);
}
public static <In extends Number, Out extends Number> Out test(In in, Out out) {
return out;
}
Run Code Online (Sandbox Code Playgroud)
对我来说很明显,他们不能(!)发生冲突,并且呼叫不能模棱两可.但是,对于最新版本的Java 8,以下调用失败:
Test.test(2, Integer::new);
Run Code Online (Sandbox Code Playgroud)
同 Error:(17, 16) java: reference to test is ambiguous
both method <In,Out>test(In,java.util.function.Function<In,Out>) in org.test and method <In,Out>test(In,Out) in org.test match
而
Test.test(2, new Function<Integer, Number>() {
@Override
public Number apply(Integer integer) {
return 10;
} …Run Code Online (Sandbox Code Playgroud) 我有以下型号:
public class WeightChange {
private float value;
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
和收藏:
private List<WeightChange> weightChanges;
Run Code Online (Sandbox Code Playgroud)
我已经实现了使用Java 8功能获得平均权重值的函数:
public float getAvgChangedWeight() {
return (float) weightChanges.stream().mapToDouble(WeightChange::getValue).average().getAsDouble();
}
Run Code Online (Sandbox Code Playgroud)
你能不能帮助改进它,因为我不认为铸造加倍是一个好主意.
当weightChanges集合为空时,它也会抛出异常.在这种情况下如何改进它?
我正在尝试使用Java 8流和lambda表达式进行顺序搜索.这是我的代码
List<Integer> list = Arrays.asList(10, 6, 16, 46, 5, 16, 7);
int search = 16;
list.stream().filter(p -> p == search).forEachOrdered(e -> System.out.println(list.indexOf(e)));
Run Code Online (Sandbox Code Playgroud)
Output: 2
2
Run Code Online (Sandbox Code Playgroud)
我知道list.indexOf(e)总是打印第一次出现的索引.如何打印所有索引?
我今天读过这篇关于lambdas的文章:
http://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood
文章建议,lambda 不是 作为anon内部类实现的(由于性能).它给出了一个示例,可以将lambda表达式编译为类的(静态)方法.
我试过一个非常简单的片段:
private void run() {
System.out.println(this);
giveHello(System.out::println);
}
private void giveHello(Consumer<String> consumer) {
System.out.println(consumer);
consumer.accept("hello");
}
Run Code Online (Sandbox Code Playgroud)
输出是:
sample.Main@14ae5a5
sample.Main$$Lambda$1/168423058@4a574795
hello
Run Code Online (Sandbox Code Playgroud)
所以它不是同一个实例.它不是一些中央的"Lambda Factory"实例.
那么lambdas是如何实现的?
java ×10
java-8 ×10
lambda ×4
java-stream ×3
accumulator ×1
generics ×1
java-7 ×1
javafx-2 ×1
javafx-8 ×1
list ×1
local-class ×1
reduce ×1