假设我有一个void方法,它只对对象进行转换,而不返回任何值,我想在流map()函数的上下文中使用它,如下所示:
public List<MyObject> getList(){
List<MyObject> objList = ...
return objList.stream().map(e -> transform(e, e.getUuid())).collect(Collectors.toList());
}
private void transform(MyObject obj, String value){
obj.setUuid("prefix" + value);
}
Run Code Online (Sandbox Code Playgroud)
这个例子是为了简单起见 - 实际的方法是做其他事情,而不仅仅是弄乱对象的UUID.
无论如何,如何在上述场景中使用void方法?当然,我可以让方法返回转换后的对象,但这是除了点之外并且违反了设计(方法应该是无效的).
我正在尝试将2D列表转换为2D int数组.但是,似乎我只能收集对象,而不是基元.
当我做:
data.stream().map(l -> l.stream().toArray(int[]::new)).toArray(int[][]::new);
Run Code Online (Sandbox Code Playgroud)
我得到编译时错误Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>).
但是,如果我int[]改为Integer[],则编译.我如何才能使用它int?
我有一个带有项目的List,其中每个项目都是一个对象,我有一个我感兴趣的getter方法.我想在完整列表上运行以总结所有这些getter结果.
当我使用java 8流时,它看起来像这样:
double currentProduction = itemList.stream().collect(
Collectors.summingDouble((e) -> e.getProduction(param)));
Run Code Online (Sandbox Code Playgroud)
在普通的旧java中,它看起来像这样:
for (Item item : itemList) {
currentProduction += item.getProduction(param);
}
Run Code Online (Sandbox Code Playgroud)
两种方法产生完全相同的结果,但我的记录器报告,对于每个项目实例,在java 8流解决方案的情况下,getProduction()方法运行TWICE.在普通的旧java列表迭代解决方案中,getProduction方法只是按预期运行一次.
由于getProduction方法成本很高,这对我来说是一个问题.
为什么是这样?我能做些什么(除了只使用for循环)?
为什么Java的没有选择这个签名<T> Stream <T> Stream.generate (Supplier <? extends T> supplier)在这一个<T> Stream <T> Stream.generate (Supplier <T> supplier)?
我的意思是下面的例子(不编译)是正确的,因为字符串的供应商在一个密码序列流中也是有效的吗?
Supplier <String> constantHello = () -> "Hello";
long count = Stream.<CharSequence>generate(constantHello).count();
Run Code Online (Sandbox Code Playgroud) 我想从地图中删除value为空的所有条目.看起来似乎并不复杂,但我试图找到一个更好的解决方案.
输入:
我有以下地图:
Map<String, Function<String, Optional<String>>> attributesToCalculate = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
其中key - 只是一个String和value - 对返回Optional <String>的方法的引用
输出:
结果,我想得到
Map<String, String> calculatedAttributes
Run Code Online (Sandbox Code Playgroud)
(不包括值为空的条目可选)
这是我的解决方案
return attributesToCalculate.entrySet()
.stream()
.map(entry -> Pair.of(entry.getKey(), entry.getValue().apply(someString)))
.filter(entry -> entry.getValue().isPresent())
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get()));
Run Code Online (Sandbox Code Playgroud)
但是我不喜欢.filter部分因为那时我必须在collect部分中的Optional上调用.get().
是否有更好的方法(可能没有.get调用)来解决这个问题?谢谢.
我是Java 8的新手.我有一个类型为A的自定义对象列表,其中A如下所示:
class A {
int id;
String name;
}
Run Code Online (Sandbox Code Playgroud)
我想确定该列表中的所有对象是否具有相同的名称.我可以通过迭代列表并捕获名称的先前和当前值来实现.在那个上下文中,我找到了如何计算列表中自定义对象的数量,这些对象的一个属性具有相同的值.但是有没有更好的方法在使用流的java 8中做同样的事情?
在java.util.stream.Stream接口中,
<R> R collect(Supplier<R> supplier,
BiConsumer<R, ? super T> accumulator,
BiConsumer<R, R> combiner);
Run Code Online (Sandbox Code Playgroud)
组合器是a BiConsumer<R, R>,而在
<R, A> R collect(Collector<? super T, A, R> collector);
Run Code Online (Sandbox Code Playgroud)
组合器是一个BinaryOperator<A>只是一个BiFunction<A,A,A>.
虽然后面的形式清楚地定义了组合后组合对象的参考,但前一种形式却没有.
那么任何Stream实现库如何知道,前一种情况下的组合对象是什么?
当我在下面运行Julia代码时,出现了错误:UndefVarError: globalValue not defined.
我认为globalValue是一个全局变量,但事实并非如此.因此,如果我在for循环中添加命令"global globalValue",我的代码将起作用.那么,有谁可以看看它让我知道发生了什么?提前致谢!
globalValue = 1.0;
tempValue = 0.1;
for ii = 1:10
# global globalValue; if I add this command, my code will work
tempValue = 5.0; ## I have a function to update "tempValue"
if globalValue < tempValue
globalValue = tempValue;
end
end
Run Code Online (Sandbox Code Playgroud) 有没有办法java.net.http.HttpClient立即释放它所持有的资源?
在内部,它包含一个选择器,一个连接池和一个Executor(当使用默认值时).但它没有实现Closeable/ AutoCloseable.
以下代码在完成并行处理后并未将所有元素放入目标列表中。这有什么原因吗?
public static void main(String[] args) {
List<Integer> source =new ArrayList<>();
List<Integer> destination = new ArrayList<>();
IntStream.range(0, 10000).forEach(element ->{
source.add(element);
});
//System.out.println(source.size());
source.parallelStream().forEach(c->{
destination.add(c);
});
System.out.println("Source Size:"+source.size());
System.out.println("destination size:"+destination.size());
}
Run Code Online (Sandbox Code Playgroud)
输出:源大小:10000 目标大小:4343
java ×9
java-stream ×8
java-8 ×6
collectors ×2
arraylist ×1
collections ×1
for-loop ×1
generics ×1
java-11 ×1
julia ×1
lambda ×1
optional ×1
performance ×1
primitive ×1
scope ×1