我想使用Java 8的流和lambdas将对象列表转换为Map.
这就是我在Java 7及以下版本中编写它的方法.
private Map<String, Choice> nameMap(List<Choice> choices) {
final Map<String, Choice> hashMap = new HashMap<>();
for (final Choice choice : choices) {
hashMap.put(choice.getName(), choice);
}
return hashMap;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用Java 8和Guava轻松完成此任务,但我想知道如何在没有Guava的情况下完成此操作.
在番石榴:
private Map<String, Choice> nameMap(List<Choice> choices) {
return Maps.uniqueIndex(choices, new Function<Choice, String>() {
@Override
public String apply(final Choice input) {
return input.getName();
}
});
}
Run Code Online (Sandbox Code Playgroud)
还有Java 8 lambda的番石榴.
private Map<String, Choice> nameMap(List<Choice> choices) {
return Maps.uniqueIndex(choices, Choice::getName);
}
Run Code Online (Sandbox Code Playgroud) 在Java 8中,Stream.map()和Stream.flatMap()方法有什么区别?
我刚刚开始使用Java 8 lambdas,我正在尝试实现我在函数式语言中习惯的一些东西.
例如,大多数函数式语言都有某种类型的查找函数,这些函数对序列进行操作,或者列表返回谓词所在的第一个元素true.我在Java 8中实现这一目标的唯一方法是:
lst.stream()
.filter(x -> x > 5)
.findFirst()
Run Code Online (Sandbox Code Playgroud)
然而这对我来说似乎效率低下,因为过滤器会扫描整个列表,至少根据我的理解(这可能是错误的).有没有更好的办法?
使用Java 8和lambdas,可以很容易地将集合作为流进行迭代,并且易于使用并行流.来自docs的两个例子,第二个使用parallelStream:
myShapesCollection.stream()
.filter(e -> e.getColor() == Color.RED)
.forEach(e -> System.out.println(e.getName()));
myShapesCollection.parallelStream() // <-- This one uses parallel
.filter(e -> e.getColor() == Color.RED)
.forEach(e -> System.out.println(e.getName()));
Run Code Online (Sandbox Code Playgroud)
只要我不关心顺序,使用并行是否总是有益的?人们会认为将更多核心的工作划分得更快.
还有其他考虑因素吗?什么时候应该使用并行流?什么时候应该使用非并行?
(这个问题被要求引发关于如何以及何时使用并行流的讨论,而不是因为我认为总是使用它们是一个好主意.)
在Java 8中,以下哪项更好?
Java 8:
joins.forEach(join -> mIrc.join(mSession, join));
Run Code Online (Sandbox Code Playgroud)
Java 7:
for (String join : joins) {
mIrc.join(mSession, join);
}
Run Code Online (Sandbox Code Playgroud)
我有很多可以用lambdas"简化"的for循环,但使用它们真的有什么好处吗?它会改善他们的性能和可读性吗?
编辑
我还将这个问题扩展到更长的方法.我知道你不能从lambda中返回或破坏父函数,在比较它们时也应该考虑到这一点,但还有什么需要考虑的吗?
我正在玩Java 8 lambdas来轻松过滤集合.但我没有找到一种简洁的方法来将结果作为新列表检索到同一语句中.到目前为止,这是我最简洁的方法:
List<Long> sourceLongList = Arrays.asList(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);
List<Long> targetLongList = new ArrayList<>();
sourceLongList.stream().filter(l -> l > 100).forEach(targetLongList::add);
Run Code Online (Sandbox Code Playgroud)
网上的例子没有回答我的问题,因为他们停止而没有生成新的结果列表.必须有一个更简洁的方式.我本来期望的是,Stream类有方法为toList(),toSet(),...
有没有办法targetLongList可以直接由第三行分配变量?
在Java 8中,如何Stream通过检查每个对象的属性的清晰度来使用API 过滤集合?
例如,我有一个Person对象列表,我想删除具有相同名称的人,
persons.stream().distinct();
Run Code Online (Sandbox Code Playgroud)
将使用Person对象的默认相等检查,所以我需要像,
persons.stream().distinct(p -> p.getName());
Run Code Online (Sandbox Code Playgroud)
不幸的是,该distinct()方法没有这种过载.如果不修改类中的相等性检查,Person是否可以简洁地执行此操作?
我有一个返回的界面java.lang.Iterable<T>.
我想使用Java 8 Stream API来操纵该结果.
但是Iterable不能"流".
知道如何将Iterable用作Stream而不将其转换为List吗?
是否可以为Java 8 并行流指定自定义线程池?我找不到任何地方.
想象一下,我有一个服务器应用程序,我想使用并行流.但是应用程序很大且是多线程的,因此我想将它划分为区分.我不想在另一个模块的应用程序块任务的一个模块中执行缓慢的任务.
如果我不能为不同的模块使用不同的线程池,这意味着我无法在大多数现实情况下安全地使用并行流.
请尝试以下示例.在单独的线程中执行一些CPU密集型任务.这些任务利用并行流.第一个任务被破坏,因此每个步骤需要1秒(通过线程休眠模拟).问题是其他线程卡住并等待损坏的任务完成.这是一个人为的例子,但想象一下servlet应用程序和有人向共享fork连接池提交长时间运行的任务.
public class ParallelTest {
public static void main(String[] args) throws InterruptedException {
ExecutorService es = Executors.newCachedThreadPool();
es.execute(() -> runTask(1000)); //incorrect task
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.shutdown();
es.awaitTermination(60, TimeUnit.SECONDS);
}
private static void runTask(int delay) {
range(1, 1_000_000).parallel().filter(ParallelTest::isPrime).peek(i -> Utils.sleep(delay)).max()
.ifPresent(max -> System.out.println(Thread.currentThread() + " " + max));
}
public static boolean isPrime(long n) {
return n > 1 && rangeClosed(2, (long) sqrt(n)).noneMatch(divisor …Run Code Online (Sandbox Code Playgroud) java-8 ×10
java-stream ×10
java ×9
collections ×2
lambda ×2
arrays ×1
concurrency ×1
for-loop ×1
iterable ×1