有人可以帮助我理解为什么这段代码的行为如评论中所述
// 1) compiles
List<Integer> l = Stream.of(1, 2, 3).collect(ArrayList::new, ArrayList::add, ArrayList<Integer>::addAll);
/*
* 2) does not compile
*
* Exception in thread "main" java.lang.Error: Unresolved compilation problems:
* Type mismatch: cannot convert from Object to <unknown>
* The type ArrayList does not define add(Object, Integer) that is applicable here
* The type ArrayList does not define addAll(Object, Object) that is applicable here
*/
Stream.of(1, 2, 3).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
// 3) compiles
Stream.of(1, 2, 3).collect(ArrayList<Integer>::new, ArrayList::add, ArrayList::addAll);
/*
* 4) …Run Code Online (Sandbox Code Playgroud) 我使用parallelStream来获取数组中最长的字符串,代码跟随,每次运行时,我得到不同的结果.AtomicReference假设即使在parallelStream中使用时也是线程安全的?但为什么会这样呢?
public static void main(String[] args) {
AtomicReference<String> longest = new AtomicReference<>();
LongAccumulator accumulator = new LongAccumulator(Math::max, 0);
List<String> words = Arrays.asList("him", "he", "thanks", "strings", "congratulations", "platform");
words.parallelStream().forEach(
next -> longest.updateAndGet(
current -> {
String result = next.length() > accumulator.intValue() ? next : current;
accumulator.accumulate(next.length());
return result;
}
)
);
System.out.println(longest.get());
}
Run Code Online (Sandbox Code Playgroud)
有一段时间,我打印出"祝贺",有时我打印出"平台".
我想知道是否有办法添加针对方法 takeWhile() 的条件测试的流的最后一个元素。我相信我想实现类似于 RxJava 的 takeUntil() 方法的东西。
我猜没有直接的方法可以做到这一点(如果我错了,请纠正我),但我想知道是否有适当的解决方法来实现这一点,我现在知道了。
我在 Stackoverflow 中进行了搜索,但几乎没有成功。如果您认为有可以解决我的问题的线程,我当然希望看到它。
如果您查看以下代码的 peek(),您将看到数字 5 已根据 takeWhile() 条件进行检查,但它从未到达 forEach() 中:
IntStream.of(1, 3, 2, 5, 4, 6)
.peek(foo -> System.out.println("Peek: " + foo))
.takeWhile(n -> n < 5)
.forEach(bar -> System.out.println("forEach: " + bar));
Run Code Online (Sandbox Code Playgroud)
预期结果是根据 takeWhile 的条件检查的最后一个元素到达 forEach 的 System.out::println。在这种情况下,它是 5。
谢谢大家!
我试图使用java 8的流来计算使用前两个值的值的乘法.我想调用一个返回数组/列表/集合的函数.我正在创建一个List并向其添加1,2.
假设列表名称是结果.
public static void main (String[] args) {
List<Integer> result = new ArrayList<Integer>();
result.add(1);
result.add(2);
int n = 5; //n can be anything, choosing 5 for this example
res(n, result);
//print result which should be [1, 2, 2, 4, 8]
}
public static List<Integer> res(int n, List<Integer> result ) {
result.stream()
.limit(n)
.reduce(identity, (base,index) -> base);
//return result;
}
Run Code Online (Sandbox Code Playgroud)
现在问题是尝试将结果传递到流中以使用流继续使用新值更新列表.根据java教程,尽管效率低下,但它是可能的.
"如果你的reduce操作涉及向集合添加元素,那么每次你的累加器函数处理一个元素时,它都会创建一个包含该元素的新集合,这是低效的."
我是否需要使用可选的第三个参数BinaryOperator组合来组合列表+结果?
<U> U reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner)
Run Code Online (Sandbox Code Playgroud)
简而言之; 我想传递一个包含两个值的列表,并让函数找到前两个值的乘法(1,2),将其添加到列表中,并找到最后两个值的乘法(2,2),并添加它到列表中,直到流达到限制.
类文件格式是否为使用变量的最终关键字提供支持?
或者它只是从代码中推断出变量的有效终结性而JIT编译器基于它执行优化?
这里,在类文件格式文档中,他们提到了关于final关键字,但仅限于将其与final块和final类一起使用的情况.最终变量
没有任何内容.
用于在流中组合两组数据.
Stream.concat(stream1, stream2).collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
要么
stream1.collect(Collectors.toSet())
.addAll(stream2.collect(Collectors.toSet()));
Run Code Online (Sandbox Code Playgroud)
哪个更有效,为什么?
如何将此代码简化为单个lambda表达式?我的想法是有一个地图列表,我想创建一个新的地图列表,使用密钥过滤器.在这个例子中,我想重新映射它,使它只保留键"x"和"z".
Map<String, String> m0 = new LinkedHashMap<>();
m0.put("x", "123");
m0.put("y", "456");
m0.put("z", "789");
Map<String, String> m1 = new LinkedHashMap<>();
m1.put("x", "000");
m1.put("y", "111");
m1.put("z", "222");
List<Map> l = new ArrayList<>(Arrays.asList(m0, m1));
List<Map> tx = new ArrayList<>();
for(Map<String, String> m : l) {
Map<String, String> filtered = m.entrySet()
.stream()
.filter(map -> map.getKey().equals("x") || map.getKey().equals("z"))
.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
tx.add(filtered);
}
System.err.println("l: " + l);
System.err.println("tx: " + tx);
Run Code Online (Sandbox Code Playgroud)
输出:
l: [{x=123, y=456, z=789}, {x=000, y=111, z=222}]
tx: [{x=123, z=789}, …Run Code Online (Sandbox Code Playgroud) 我正在使用Stream并行处理,并了解如果我使用平面阵列流,它会得到非常快速的处理.但如果我使用ArrayList,那么处理速度会慢一些.但是,如果我使用LinkedList或使用一些二进制树,处理速度会更慢.
所有听起来更像是流的可分割性,处理速度越快.这意味着阵列和数组列表在并行流的情况下最有效.这是真的吗?如果是这样,ArrayList如果我们想并行处理流,我们总是使用或者Array吗?如果是这样,如何使用LinkedList和BlockingQueue并行流?
另一件事是选择的中间函数的状态.如果我执行像无状态操作filter(),map(),性能高,但如果执行像国家提供充分的操作distinct(),sorted(),limit(),skip(),它需要大量的时间.再次,并行流变慢.这是否意味着我们不应该在并行流中使用状态全中间函数?如果是这样,那么解决这个问题的方法是什么?
微软是否为每个单独的平台操作系统重写了 CLR,或者它们只是相同的代码?
首先,一些上下文代码:
import java.util.*;
import java.util.concurrent.atomic.DoubleAdder;
import java.util.function.Function;
import java.util.stream.Collectors;
class Scratch {
static enum Id {A, B, C}
static class IdWrapper {
private final Id id;
public IdWrapper(Id id) {this.id = id;}
Id getId() { return id; }
}
public static void main(String[] args) {
Map<String, Object> v1 = new HashMap<>();
v1.put("parents", new HashSet<>(Arrays.asList(new IdWrapper(Id.A), new IdWrapper(Id.B))));
v1.put("size", 1d);
Map<String, Object> v2 = new HashMap<>();
v2.put("parents", new HashSet<>(Arrays.asList(new IdWrapper(Id.B), new IdWrapper(Id.C))));
v2.put("size", 2d);
Map<String, Map<String, Object>> allVs = new HashMap<>(); …Run Code Online (Sandbox Code Playgroud)