我试图在下面的Collectors.toMap()调用中为"keyMapper"函数参数提供一个更简洁的表达式:
List<Person> roster = ...;
Map<String, Person> map =
roster
.stream()
.collect(
Collectors.toMap(
new Function<Person, String>() {
public String apply(Person p) { return p.getLast(); }
},
Function.<Person>identity()));
Run Code Online (Sandbox Code Playgroud)
似乎我应该能够使用lambda表达式来内联它,但我无法想出一个编译它的人.(我对lambdas很新,所以这并不奇怪.)
谢谢.
- >更新:
如接受的答案所述
Person::getLast
Run Code Online (Sandbox Code Playgroud)
是我在寻找的东西,也是我尝试过的东西.然而,Eclipse 4.3的BETA_8每晚构建是问题 - 它标记为错误.从命令行编译时(我应该在发布之前完成),它起作用了.所以,是时候用eclipse.org提交bug了.
谢谢.
考虑我有以下代码:
class Foo {
Y func(X x) {...}
void doSomethingWithAFunc(Function<X,Y> f){...}
void hotFunction(){
doSomethingWithAFunc(this::func);
}
}
Run Code Online (Sandbox Code Playgroud)
假设hotFunction经常被调用.那么缓存是否可取this::func,也许是这样的:
class Foo {
Function<X,Y> f = this::func;
...
void hotFunction(){
doSomethingWithAFunc(f);
}
}
Run Code Online (Sandbox Code Playgroud)
就我对java方法引用的理解而言,虚拟机在使用方法引用时会创建匿名类的对象.因此,缓存引用将仅创建该对象一次,而第一种方法在每个函数调用上创建它.它是否正确?
是否应缓存出现在代码中热位置的方法引用,或者VM是否能够对此进行优化并使缓存变得多余?是否存在关于此的一般最佳实践,或者这种高度VM实现是否特定于此类缓存是否有用?
是否tomcat7-maven-plugin使用tomcat 8服务器和java 8?我找不到任何东西tomcat8-maven-plugin.
我正在尝试将每个循环的旧常规转换为java7到java8的每个循环用于映射条目集但是我收到错误.这是我要转换的代码:
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
Run Code Online (Sandbox Code Playgroud)
这是我所做的改变:
map.forEach( Map.Entry<String, String> entry -> {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
});
Run Code Online (Sandbox Code Playgroud)
我也尝试过这样做:
Map.Entry<String, String> entry;
map.forEach(entry -> {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
});
Run Code Online (Sandbox Code Playgroud)
但仍面临错误.我得到的错误是:Lambda表达式的签名与功能接口方法的签名不匹配accept(String, String)
Java 8中新的Date Time API的一个特性应该是纳秒精度.但是,当我将当前日期时间打印到控制台时,就像这样
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ");
System.out.println(OffsetDateTime.now().format(formatter));
Run Code Online (Sandbox Code Playgroud)
我只看到毫秒精度:2015-11-02T12:33:26,746000000 + 0100
操作系统似乎确实支持纳秒精度.当我通过终端打印当前日期时间
date -Ins
Run Code Online (Sandbox Code Playgroud)
我看到2015-11-02T12:33:26,746134417 + 0100
如何在Java中获得纳秒精度?我在Ubuntu 14.04 64位上运行Oracle Java 1.8.0_66
我有以下情况
Map<Key, ListContainer> map;
public class ListContainer{
List<AClass> lst;
}
Run Code Online (Sandbox Code Playgroud)
我已经合并所有列表lst从ListContainer从对象Map地图.
public static void main(String args[]){
List<AClass> alltheObjectsAClass = map.values().stream(). // continue....
}
Run Code Online (Sandbox Code Playgroud)
任何想法,使用Java 8流API?
Stream没有last()方法:
Stream<T> stream;
T last = stream.last(); // No such method
Run Code Online (Sandbox Code Playgroud)
获取最后一个元素的最优雅和/或最有效的方法是什么(空流为null)?
前段时间,我发表了一篇关于递归计算斐波纳契数的Java 8函数式方法的博文,其中包含一个ConcurrentHashMap缓存和新的有用computeIfAbsent()方法:
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Test {
static Map<Integer, Integer> cache = new ConcurrentHashMap<>();
public static void main(String[] args) {
System.out.println(
"f(" + 8 + ") = " + fibonacci(8));
}
static int fibonacci(int i) {
if (i == 0)
return i;
if (i == 1)
return 1;
return cache.computeIfAbsent(i, (key) -> {
System.out.println(
"Slow calculation of " + key);
return fibonacci(i - 2) + fibonacci(i - 1);
});
}
} …Run Code Online (Sandbox Code Playgroud) 我知道这些方法的执行顺序不同,但在我的所有测试中,我都无法实现不同的顺序执行.
例:
System.out.println("forEach Demo");
Stream.of("AAA","BBB","CCC").forEach(s->System.out.println("Output:"+s));
System.out.println("forEachOrdered Demo");
Stream.of("AAA","BBB","CCC").forEachOrdered(s->System.out.println("Output:"+s));
Run Code Online (Sandbox Code Playgroud)
输出:
forEach Demo
Output:AAA
Output:BBB
Output:CCC
forEachOrdered Demo
Output:AAA
Output:BBB
Output:CCC
Run Code Online (Sandbox Code Playgroud)
请提供两种方法产生不同输出的示例.
已安装Android Studio 2.2 Preview 2并收到此错误消息:
错误:CreateProcess error = 216,此版本的%1与您正在运行的Windows版本不兼容.检查计算机的系统信息,然后与软件发行商联系
我重新安装了JDK,仍然是同样的问题.
我甚至尝试使用嵌入式JDK,但仍然遇到同样的问题: