Java 8 stream api非常好用,我非常喜欢它.让我紧张的一件事是90%的时间我想作为集合输入作为集合和输出.结果是我必须一直打电话stream()和collect()方法:
collection.stream().filter(p->p.isCorrect()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
是否有任何java api可以让我跳过流并直接操作集合(比如linqc#?):
collection.filter(p->p.isCorrect)
Run Code Online (Sandbox Code Playgroud) 如何计算Java 8中List的单词频率?
List <String> wordsList = Lists.newArrayList("hello", "bye", "ciao", "bye", "ciao");
Run Code Online (Sandbox Code Playgroud)
结果必须是:
{ciao=2, hello=1, bye=2}
Run Code Online (Sandbox Code Playgroud) 如何在Java 8 Stream上实现"分区"操作?通过分区,我的意思是,将流划分为给定大小的子流.不知何故,它将与Guava Iterators.partition()方法完全相同,只是希望分区是懒惰评估的Streams而不是List的.
作为我研究在流中使用复杂过滤器或多个过滤器之间区别的一部分,我注意到Java 12的性能比Java 8慢。
这些奇怪的结果有什么解释吗?我在这里想念什么吗?
组态:
Java 8
Java 12
VM选项: -XX:+UseG1GC -server -Xmx1024m -Xms1024m
JMH吞吐量结果:
流+复杂过滤器
public void complexFilter(ExecutionPlan plan, Blackhole blackhole) {
long count = plan.getDoubles()
.stream()
.filter(d -> d < Math.PI
&& d > Math.E
&& d != 3
&& d != 2)
.count();
blackhole.consume(count);
}
Run Code Online (Sandbox Code Playgroud)
流+多个过滤器
public void multipleFilters(ExecutionPlan plan, Blackhole blackhole) {
long count = plan.getDoubles()
.stream() …Run Code Online (Sandbox Code Playgroud) 有时我想在流上执行一组操作,然后以不同的方式处理结果流与其他操作.
我是否可以这样做而无需指定两次常见的初始操作?
例如,我希望dup()存在以下方法:
Stream [] desired_streams = IntStream.range(1, 100).filter(n -> n % 2 == 0).dup();
Stream stream14 = desired_streams[0].filter(n -> n % 7 == 0); // multiples of 14
Stream stream10 = desired_streams[1].filter(n -> n % 5 == 0); // multiples of 10
Run Code Online (Sandbox Code Playgroud) 我正在玩Java 8的流,无法理解我得到的性能结果.我有2个核心CPU(Intel i73520M),Windows 8 x64和64位Java 8更新5.我正在做简单的字符串流/并行流Strings,发现并行版本有点慢.
Function<Stream<String>, Long> timeOperation = (Stream<String> stream) -> {
long time1 = System.nanoTime();
final List<String> list =
stream
.map(String::toLowerCase)
.collect(Collectors.toList());
long time2 = System.nanoTime();
return time2 - time1;
};
Consumer<Stream<String>> printTime = stream ->
System.out.println(timeOperation.apply(stream) / 1000000f);
String[] array = new String[1000000];
Arrays.fill(array, "AbabagalamagA");
printTime.accept(Arrays.stream(array)); // prints around 600
printTime.accept(Arrays.stream(array).parallel()); // prints around 900
Run Code Online (Sandbox Code Playgroud)
考虑到我有2个CPU内核的事实,并行版本不应该更快吗?有人能给我一个暗示为什么并行版本更慢?
为了更好地理解新流API我试图转换一些旧代码,但我坚持这个.
public Collection<? extends File> asDestSet() {
HashMap<IFileSourceInfo, Set<File>> map = new HashMap<IFileSourceInfo, Set<File>>();
//...
Set<File> result = new HashSet<File>();
for (Set<File> v : map.values()) {
result.addAll(v);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法为它创建一个有效的收集器:
public Collection<? extends File> asDestSet() {
HashMap<IFileSourceInfo, Set<File>> map = new HashMap<IFileSourceInfo, Set<File>>();
//...
return map.values().stream().collect(/* what? */);
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
System.out.println("#1");
Stream.of(0, 1, 2, 3)
.peek(e -> System.out.println(e))
.sorted()
.findFirst();
System.out.println("\n#2");
IntStream.range(0, 4)
.peek(e -> System.out.println(e))
.sorted()
.findFirst();
Run Code Online (Sandbox Code Playgroud)
输出将是:
#1
0
1
2
3
#2
0
Run Code Online (Sandbox Code Playgroud)
谁能解释一下,为什么两个流的输出不同?
我有以下代码:
List<Object> list = new ArrayList<>();
list.addAll(method1());
if(list.isEmpty()) { list.addAll(method2()); }
if(list.isEmpty()) { list.addAll(method3()); }
if(list.isEmpty()) { list.addAll(method4()); }
if(list.isEmpty()) { list.addAll(method5()); }
if(list.isEmpty()) { list.addAll(method6()); }
return list;
Run Code Online (Sandbox Code Playgroud)
是否有一种很好的方式有条件地添加元素,可能使用流操作?我想只在列表为空时添加来自method2的元素,否则返回等等.
编辑:值得一提的是,这些方法包含大量逻辑,因此需要防止执行.
如何将多个谓词应用于java.util.Stream's filter()方法?
这就是我现在所做的,但我并不喜欢它.我有一些Collection东西,我需要根据Collection过滤器(谓词)减少事物的数量:
Collection<Thing> things = someGenerator.someMethod();
List<Thing> filtered = things.parallelStream().filter(p -> {
for (Filter f : filtersCollection) {
if (f.test(p))
return true;
}
return false;
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我知道如果我事先了解过滤器的数量,我可以这样做:
List<Thing> filtered = things.parallelStream().filter(filter1).or(filter2).or(filter3)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但是如何在不混合编程风格的情况下应用未知数量的谓词?知道它看起来有点难看......
java ×10
java-stream ×10
java-8 ×8
benchmarking ×1
collections ×1
java-12 ×1
jmh ×1
lambda ×1
performance ×1
predicate ×1
word-count ×1