我有以下型号:
public class WeightChange {
private float value;
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
和收藏:
private List<WeightChange> weightChanges;
Run Code Online (Sandbox Code Playgroud)
我已经实现了使用Java 8功能获得平均权重值的函数:
public float getAvgChangedWeight() {
return (float) weightChanges.stream().mapToDouble(WeightChange::getValue).average().getAsDouble();
}
Run Code Online (Sandbox Code Playgroud)
你能不能帮助改进它,因为我不认为铸造加倍是一个好主意.
当weightChanges集合为空时,它也会抛出异常.在这种情况下如何改进它?
以下代码是否需要包含在try-with-resources中以确保底层文件已关闭?
List<String> rows = Files.lines(inputFilePath).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我喜欢番石榴,我会继续使用番石榴.但是,在有意义的地方,我尝试使用Java 8中的"新东西" .
"问题"
让我们说我想加入url属性String.在Guava我会这样做:
Map<String, String> attributes = new HashMap<>();
attributes.put("a", "1");
attributes.put("b", "2");
attributes.put("c", "3");
// Guava way
String result = Joiner.on("&").withKeyValueSeparator("=").join(attributes);
Run Code Online (Sandbox Code Playgroud)
凡result为a=1&b=2&c=3.
题
在Java 8(没有任何第三方库)中,最优雅的方法是什么?
Java 8并行流如何在使用子句中的抛出异常上运行,例如在forEach处理中?例如,以下代码:
final AtomicBoolean throwException = new AtomicBoolean(true);
IntStream.range(0, 1000)
.parallel()
.forEach(i -> {
// Throw only on one of the threads.
if (throwException.compareAndSet(true, false)) {
throw new RuntimeException("One of the tasks threw an exception. Index: " + i);
});
Run Code Online (Sandbox Code Playgroud)
它会立即停止处理元素吗?是否等待已经启动的元素完成?是否等待所有流完成?抛出异常后是否开始处理流元素?
什么时候回来?异常后立即?毕竟/部分元素是由消费者处理的?
在并行流引发异常后,是否继续处理元素?(发现这种情况发生的情况).
这里有一般规则吗?
编辑(15-11-2016)
试图确定并行流是否提前返回,我发现它不是确定的:
@Test
public void testParallelStreamWithException() {
AtomicInteger overallCount = new AtomicInteger(0);
AtomicInteger afterExceptionCount = new AtomicInteger(0);
AtomicBoolean throwException = new AtomicBoolean(true);
try {
IntStream.range(0, 1000)
.parallel()
.forEach(i -> {
overallCount.incrementAndGet();
afterExceptionCount.incrementAndGet();
try …Run Code Online (Sandbox Code Playgroud) parallel-processing exception-handling exception java-8 java-stream
哪种方式最有效地处理不同的有序集合?
Set<MyObj> ret = new TreeSet<>();
for (Foo foo : foos)
ret.add(new MyObj(foo));
Run Code Online (Sandbox Code Playgroud)
List<MyObj> ret = foos.stream().map(MyObj::new)
.distinct().sorted()
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
Set<MyObj> ret = foos.stream().map(MyObj::new)
.collect(Collectors.toCollection(TreeSet::new));
Run Code Online (Sandbox Code Playgroud)
第一种方式似乎是最不优雅但易于阅读的.第二种方式让我担心distinct并且sorted会两次处理流.最后一种方式感觉还可以,但是流中的TreeSet开销是多少?
有线索吗?谢谢.
我想获取输入并在其上应用并行流,然后我想输出为列表.输入可以是我们可以应用流的任何列表或任何集合.
我担心的是,如果我们想要输出作为映射它们,我们有一个来自java的选项就像
list.parallelStream().collect(Collectors.toConcurrentMap(args))
Run Code Online (Sandbox Code Playgroud)
但是我没有选择以线程安全的方式从并行流中收集以提供列表作为输出.我看到另外一个选项可供使用
list.parallelStream().collect(Collectors.toCollection(<Concurrent Implementation>))
通过这种方式,我们可以在collect方法中提供各种并发实现.但我认为java.util.concurrent中只存在CopyOnWriteArrayList List实现.我们可以在这里使用各种队列实现,但那些不会像列表一样.我的意思是我们可以解决这个问题.
如果我想要输出列表,你能指导一下最好的方法吗?
注意:我找不到与此相关的任何其他帖子,任何参考都会有所帮助.
int[] ar1 = {1,2,3};
int[] ar2 = {1,2,3};
Run Code Online (Sandbox Code Playgroud)
输出:
{2,3,4,3,4,5,4,5,6}
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
IntStream.range(0,ar1.length).map(x -> IntStream.range(0,ar2.length).map(y -> ar1[y]+ar2[x]));
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
类型不匹配:无法转换
IntStream为int.
如何使用流将Java 8中的每个值ar1与每个值相加ar2?
我希望实现这样的目标:
items.stream()
.filter(s-> s.contains("B"))
.forEach(s-> s.setState("ok"))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
过滤,然后从过滤结果中更改属性,然后将结果收集到列表中.但是,调试器说:
无法
collect(Collectors.toList())在原始类型上调用void.
我需要2个流吗?
考虑以下代码:
public static void main(String[] args) {
Stream.iterate(1, i -> i + 1)
.flatMap(i -> Stream.of(i, i, i))
.peek(System.out::println)
.limit(4)
.forEach(i -> {});
}
Run Code Online (Sandbox Code Playgroud)
Java 8的输出:
1
1
1
2
2
2
Run Code Online (Sandbox Code Playgroud)
在Java 11中:
1
1
1
2
Run Code Online (Sandbox Code Playgroud)
这是Java 8中的错误或预期的行为,在11中进行了更改吗?
上面的代码只是演示不同行为的示例,但是更重要的含义是,以下代码在Java 11中显示1,2,3,但在Java 8中进入了无限循环:
Stream.iterate(0, i -> i + 10)
.flatMap(i -> Stream.iterate(i + 1, j -> j + 1))
.limit(3)
.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud) 我如何使用收集器将DAO列表转换为 Map<String, List<Pojo>>
daoList看起来像这样:
[0] : id = "34234", team = "gools", name = "bob", type = "old"
[1] : id = "23423", team = "fool" , name = "sam", type = "new"
[2] : id = "34342", team = "gools" , name = "dan", type = "new"
Run Code Online (Sandbox Code Playgroud)
我想对“团队”属性进行分组,并为每个团队提供一个列表,如下所示:
"gools":
["id": 34234, "name": "bob", "type": "old"],
["id": 34342, "name": "dan", "type": "new"]
"fool":
["id": 23423, "name": "sam", "type": "new"]
Run Code Online (Sandbox Code Playgroud)
Pojo看起来像这样:
@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class Pojo{
private String …Run Code Online (Sandbox Code Playgroud) java-stream ×10
java ×9
java-8 ×9
collectors ×1
dictionary ×1
exception ×1
file-io ×1
flatmap ×1
generics ×1
java-11 ×1
lambda ×1
performance ×1
treeset ×1