现在我只能实现两个集合的笛卡尔积,这里是代码:
public static <T1, T2, R extends Collection<Pair<T1, T2>>>
R getCartesianProduct(
Collection<T1> c1, Collection<T2> c2,
Collector<Pair<T1, T2>, ?, R> collector) {
return c1.stream()
.flatMap(e1 -> c2.stream().map(e2 -> new Pair<>(e1, e2)))
.collect(collector);
}
Run Code Online (Sandbox Code Playgroud)
这段代码在IntelliJ中工作正常,但在Eclipse中没有(编译器合规级别为1.8):
The method collect(Collector<? super Object,A,R>)
in the type Stream<Object> is not applicable for
the arguments (Collector<Pair<T1,T2>,capture#5-of ?,R>)
Run Code Online (Sandbox Code Playgroud)
这是Pair.java:
public class Pair<T1, T2> implements Serializable {
protected T1 first;
protected T2 second;
private static final long serialVersionUID = 1360822168806852921L;
public Pair(T1 first, T2 second) …Run Code Online (Sandbox Code Playgroud) 所以,最后从Java 6跳到Java 8,我已经阅读了相当数量的Java 8 Streams API.不幸的是,几乎所有被问到的例子几乎都接近于我想要弄清楚怎么做,但还不够接近.
我拥有的是什么
final List<Function<? super Double, Double>> myList = generateList();
final double myVal = calculate(10);
private double calculate(double val) {
for (Function<? super Double, Double> function : this.myList) {
val += function.apply(val);
}
return val;
}
Run Code Online (Sandbox Code Playgroud)
现在,我已经明白我可以做类似的事情.stream().forEach(),但只适用于foreach和stream需要最终变量.我试图探索一下DoubleStream得到一个sum(),但我需要重新应用当前的和每个Function并将该总和添加到下一个函数,如上面的代码示例所示.
纯流API可以实现这一点吗?
编辑:因此,在对该reduce()区域进行测试后,我对执行此类计算所花费的时间进行了简单测试,结果不支持流.这是一个示例https://gist.github.com/gabizou/33f616c08bde5ab97e56.包括来自相当基本测试的日志输出.
这是代码:
public class LogService {
private final BlockingQueue<String> queue;
private final LoggerThread loggerThread;
private final PrintWriter writer;
@GuardedBy("this") private boolean isShutdown;
@GuardedBy("this") private int reservations; // <-- counter
public void start() { loggerThread.start(); }
public void stop() {
synchronized (this) { isShutdown = true; }
loggerThread.interrupt();
}
public void log(String msg) throws InterruptedException {
synchronized (this) {
if (isShutdown)
throw new IllegalStateException(...);
++reservations;
}
queue.put(msg);
}
private class LoggerThread extends Thread {
public void run() {
try {
while (true) …Run Code Online (Sandbox Code Playgroud) 该ConcurrentHashMap有一对夫妇的新方法.我有两个问题:
ConcurrentMap?parallelismThreshold意思或做什么?使用Stream API获取一对一映射时遇到一些麻烦.基本上,说你上课了.
public class Item {
private final String uuid;
private Item(String uuid) {
this.uuid = uuid;
}
/**
* @return universally unique identifier
*/
public String getUuid() {
return uuid;
}
}
Run Code Online (Sandbox Code Playgroud)
我想要一个Map<String, Item>容易的查找.但鉴于此Stream<Item>,似乎没有一种简单的方法可以实现Map<String, Item>.
显然,Map<String, List<Item>>不是没有的事情:
public static Map<String, List<Item>> streamToOneToMany(Stream<Item> itemStream) {
return itemStream.collect(groupingBy(Item::getUuid));
}
Run Code Online (Sandbox Code Playgroud)
这是更安全更普遍的情况,但在这种情况下我们确实知道只会有一对一的情况.我找不到任何可编译的东西 - 我特意试图将downstream参数设置为muck Collectors.groupingBy.就像是:
// DOESN'T COMPILE
public static Map<String, Item> streamToOneToOne(Stream<Item> itemStream) {
return itemStream.collect(groupingBy(Item::getUuid, Function.identity()));
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我正在尝试使用一个流List<Map<String, Integer>>来合并包含在一个中的所有地图.这是将多个请求响应聚合为一个.
考虑这个数据集(使用JSON使其更容易阅读):
[
{"field1": 1, "field2": 5},
{"field2": 6, "field3": 10},
{"field1": 3, "field4": 15}
]
Run Code Online (Sandbox Code Playgroud)
我希望它会导致以下(顺序并没有关系):
{"field1": 4, "field2": 11, "field3": 10, "field4": 15}
Run Code Online (Sandbox Code Playgroud)
我想将所有键相加并将它们组合成一个地图.使用Java 8中的Stream接口有一个很好的方法吗?
我发现Collectors.groupingBy(o -> o, Collectors.counting()),所以也许我在正确的界限上,我只需要弄清楚如何在不搞砸结果的情况下实际合并它们.
提前致谢.
为什么当我从得到一个列表LongStream用Collectors.toList()了一个错误,但有Stream没有错误?
例子 :
错误:
Something.mapToLong(Long::parseLong).collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
正确:
Something.map(Long::valueOf).collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud) 我有以下代码
public class BenchMark {
public static void main(String args[]) {
doLinear();
doLinear();
doLinear();
doLinear();
}
private static void doParallel() {
IntStream range = IntStream.range(1, 6).parallel();
long startTime = System.nanoTime();
int reduce = range
.reduce((a, item) -> a * item).getAsInt();
long endTime = System.nanoTime();
System.out.println("parallel: " +reduce + " -- Time: " + (endTime - startTime));
}
private static void doLinear() {
IntStream range = IntStream.range(1, 6);
long startTime = System.nanoTime();
int reduce = range
.reduce((a, item) -> a * …Run Code Online (Sandbox Code Playgroud) 我想知道如果在过滤流后不存在某些值时如何做某些行为.
我们假设代码:
foo.stream().filter(p -> p.someField == someValue).findFirst().ifPresent(p -> {p.someField = anotherValue; someBoolean = true;});
Run Code Online (Sandbox Code Playgroud)
我是如何在没有价值的情况下放一些Else后的ifPresent?
orElseStream上有一些我可以调用的方法findFirst,但我看不到用这些方法做到这一点的方法orElse
你能解释一下吗?为什么
Stream.of(l1, l2).flatMap((x)->x.stream()).forEach((x)->System.out.println(x));
Run Code Online (Sandbox Code Playgroud)
和
Stream.of(l1, l2).flatMap((x)->Stream.of(x)).forEach((x)->System.out.println(x));
Run Code Online (Sandbox Code Playgroud)
是不同的?
java ×9
java-8 ×8
java-stream ×8
lambda ×2
collections ×1
collectors ×1
concurrency ×1
foreach ×1
generics ×1
jvm ×1
optional ×1
performance ×1