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)
如何在这里使用过滤功能?
假设这个:
ExecutorService service = ...;
// somewhere in the code the executorService is used this way:
service.submit(() -> { ... });
Run Code Online (Sandbox Code Playgroud)
lambda表达式默认为Callable.
有没有办法让它实例化Runnable?
谢谢你的帮助.
我有一个方法可以并行计算列表中数字的平方并将它们相加:
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)
有优雅的方式吗?我想为每个列表操作启动单独的线程并没有多大帮助,因为它们使用相同的线程池.是否有意义或是否应如上所述计算结果?
我有一个字符串和B字符串,app如果不是null则更喜欢A,并调用转换方法changeA().但是,如果A为null,则它将使用B和invoke方法changeB().两个转化方法返回相同的对象类型Result.但有一点需要注意的是,str B也可能为null,如果该情况返回null.
我如何使用JDK中的optional和lambda表达式来解决这个问题.
我有以下代码库:
changeA(){
..
}
changeB(){
..
}
aMethod(){
Optional<String> optA=Optional.ofNullable(a);
Result result = optA.map( aStr -> this.changeA( aStr ) ).orElse( this.changeB( bStr ) );
}
Run Code Online (Sandbox Code Playgroud)
这看起来有点奇怪,因为orElse方法使用的变量不在lambda的范围内.我可以知道最好的方法吗?
也许我应该说明为什么我选择了Optional.我希望我的团队的开发人员明白,即使字符串A是首选,它也可能是null,必须处理,因为代码的一部分是敏感的.字符串B仅用于记录的遗留部分.希望这澄清了我对这种方法的意图.
我想对任何不成功的响应都有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()只要它实例化一个新集合并填充它并返回结果,该方法就不必完全按照这种方式查看.
我有一个Map是一个值列表的映射,但我需要反转它以便
Map<Integer, List<String>>
Run Code Online (Sandbox Code Playgroud)
变
Map<String, List<Integer>>
Run Code Online (Sandbox Code Playgroud)
比如我有
1 -> { A, B, C }
2 -> { B }
3 -> { A, C }
Run Code Online (Sandbox Code Playgroud)
而且我想看
A -> { 1, 3 }
B -> { 1, 2 }
C -> { 1, 3 }
Run Code Online (Sandbox Code Playgroud)
在Java 8中有没有更简单的方法来执行此操作,而不是迭代遍历映射条目并创建一个不存在的集合条目,并添加到列表等?我一直认为这很明显,但我无法解决这个问题.
提前致谢
我想知道是否有更好的方法来重写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 ×10
java ×9
java-stream ×7
lambda ×6
math ×2
arrays ×1
concurrency ×1
generics ×1
hashmap ×1
optional ×1