我正在尝试转换List为HashMap使用流.这是我的代码..
attributeUnitMap = attributesList.stream()
.filter(a -> a.getAttributeUnit() != null)
.collect(Collectors.toMap(Attribute::getAttributeName, a -> a.getAttributeUnit()));
Run Code Online (Sandbox Code Playgroud)
现在我想添加条件,如果我得到属性名称null,那么应该将项目添加到带有空白字符串的map中,如下所示(any_attributeName,"").
如何使用流操作实现此目的.我知道我可以通过使用过滤条件来检查属性名称是否为null但是如果它为null则可以添加空字符串.可能吗?如果没有,为什么呢?请帮忙.
Apache commons math有一个RealVector接口支持mapToSelf流体接口,其工作方式如下:
RealVector result = v.mapAddToSelf(3.4).mapToSelf(new Tan()).mapToSelf(new Power(2.3));
Run Code Online (Sandbox Code Playgroud)
如果我有一个double[]数组,我将如何使用Java 8流和Java Math做类似的事情?必须重用相同的数组.
TIA,Ole
我有一个LinkedHashMap.
sorted:{0=[1, 2], 5=[4, 3], 1=[2, 0, 3], 2=[4, 0, 1], 3=[4, 5, 1], 4=[5, 2, 3]}
Run Code Online (Sandbox Code Playgroud)
我尝试根据每个键的大小过滤每个键的值.例如,对于条目2=[4, 0, 1],我需要过滤值,使得键只应具有大小大于或等于(>=)的值
考虑一下2=[4, 1]:因为0只有两个元素,我们将其删除.4和1有三个元素,大小等于2,所以我们保留它.
最终输出应该是:
nodes_withHighDegreee :{0=[1, 2], 5=[4, 3], 1=[2, 3], 2=[4, 1], 3=[4, 1], 4=[2, 3]}
Run Code Online (Sandbox Code Playgroud)
我试过了 :
Map<Integer, List<Integer>> nodes_withHighDegree = sorted.entrySet().stream()
.peek(e -> e.getValue().filter((a,b)-> map.get(a).size() >= map.get(b).size()))
.collect(LinkedHashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), (m0, m1) -> m0.putAll(m1));
System.out.println("After sort" + nodes_withHighDegree);
Run Code Online (Sandbox Code Playgroud)
如何在这里使用过滤功能?
我有一个方法可以并行计算列表中数字的平方并将它们相加:
public static double sumSquared(List<Double> values) {
return values
.parallelStream()
.mapToDouble(d -> d * d)
.sum();
}
Run Code Online (Sandbox Code Playgroud)
假设存在一些双重列表,如何进一步并行化,例如
double result = sumSquared(list0) + (sumSquared(list1) * sumSquared(list2));
Run Code Online (Sandbox Code Playgroud)
有优雅的方式吗?我想为每个列表操作启动单独的线程并没有多大帮助,因为它们使用相同的线程池.是否有意义或是否应如上所述计算结果?
我想对任何不成功的响应都有failfast行为,如果一切都成功,那么我将返回上一个成功的响应,如下面的代码所示.
for(int i=0;i<input.size(); i++){
Data data = service.getData(input.get(i));
if(data.isSuccessful() && i==input.size()-1){
return data;
}else if(!data.isSuccessful()){
return data;
}else{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图用流替换上面提到的代码,但是到目前为止还没能做到.主要问题是我无法模仿java8流代码中的i(索引)变量行为.
resp = input.stream().map((input)->{service.getData()}).filter(
(resp)->{
if(!resp.isSuccessful())
return true;
else if(resp.isSuccessful() && last resp)//if somehow I figure out last element
return true;
else
return false;}).findFirst();
Run Code Online (Sandbox Code Playgroud) 我想知道double[][]使用流的多个2 阵列矩阵最紧凑和有效的方法.该方法应遵循此处所示的矩阵乘法规则:http:
//www.mathwarehouse.com/algebra/matrix/multiply-matrix.php
这是使用for循环的一种方法('this'是第一个矩阵'):
final int nRows = this.getRowDimension();
final int nCols = m.getColumnDimension();
final int nSum = this.getColumnDimension();
final double[][] outData = new double[nRows][nCols];
// Will hold a column of "m".
final double[] mCol = new double[nSum];
final double[][] mData = m.data;
// Multiply.
for (int col = 0; col < nCols; col++) {
// Copy all elements of column "col" of "m" so that
// will be in contiguous memory.
for (int mRow = …Run Code Online (Sandbox Code Playgroud) 我首先要承认擦除可能会使这变得不可能.这是我正在尝试做的,这不起作用:
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Suppliers {
public static <CollectionType, T> CollectionType<T>
fill(CollectionType colltype, Supplier<T> gen, int n) {
return Stream.generate(gen)
.limit(n)
.collect(colltype::new, colltype::add, colltype::addAll);
}
}
Run Code Online (Sandbox Code Playgroud)
目标是生成一个实例,CollectionType并使用n项目填充它gen.这种形式collect()确实实例化了一个新集合,但我不清楚该类型是否可以通过参数列表传递fill().fill()只要它实例化一个新集合并填充它并返回结果,该方法就不必完全按照这种方式查看.
我想知道是否有更好的方法来重写groupA方法与lambda作为链接操作的一部分?
public class Id {
private final int value;
public Id(int value) {
this.value = value;
}
public int value() {
return value;
}
}
public class Ids implements Iterable<Id> {
private final List<Id> ids;
private Ids(List<Id> ids) {
this.ids = ids;
}
public static Ids of(List<Id> ids) {
return new Ids(ids);
}
public Ids groupA() {
return Ids.of(ids.stream()
.filter(id -> id.value() > 5)
.collect(Collectors.toList()));
}
@Override
public Iterator<Id> iterator() {
return ids.iterator();
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我想做点什么
ids.stream()
.filter(id -> id …Run Code Online (Sandbox Code Playgroud) Java 8 Stream API中的collect操作被定义为可以安全地并行执行的可变缩减,即使结果Collection不是线程安全的.
我们可以对Stream.toArray()方法说同样的话吗?
这个方法是一个可变的减少,即使Stream是并行流并且结果数组不是线程安全的,也是线程安全的吗?
我有一个unix时间戳列表,例如:
[1111111 1200000 1200060 1200120 1200180 1300000 1400000 140060]
Run Code Online (Sandbox Code Playgroud)
我希望按照彼此60秒内的簇对它们进行分组,其中键是第一个时间戳,例如:
{1111111=[1111111], 1200000=[1200000,120060, 1200120], 1300000=[1300000], 1400060=[1400000, 1400060]}
Run Code Online (Sandbox Code Playgroud)
我通过使用for循环实现了这一点,我希望有一种更好的方法,最好使用Java 8流.
(我对Java不太好,所以如果没有办法使用流,那么构造for循环是否更好?)
List <Integer> timestamps = new ArrayList<Integer>();
timestamps.add(1111111);
timestamps.add(1200000);
timestamps.add(1200060);
timestamps.add(1200120);
timestamps.add(1200180);
timestamps.add(1300000);
timestamps.add(1400000);
timestamps.add(1400060);
HashMap <Integer, List <Integer>> grouped = new HashMap<Integer, List <Integer>>();
List <Integer> subList = new ArrayList<Integer>();
for (int i = 0; i < timestamps.size(); i++) {
if(i > 0 && (timestamps.get(i - 1) + 60 < timestamps.get(i))) {
grouped.put(subList.get(0), new ArrayList <Integer>(subList));
subList.removeAll(subList);
}
subList.add(timestamps.get(i));
}
grouped.put(subList.get(0), …Run Code Online (Sandbox Code Playgroud) java-8 ×10
java-stream ×10
java ×9
lambda ×3
math ×2
arrays ×1
concurrency ×1
dictionary ×1
generics ×1
hashmap ×1
list ×1