假设我有一个天真的阶乘函数:
import java.util.stream.LongStream;
public class FactorialTest {
static long factorial(long n, boolean parallel) {
return (parallel
? LongStream.range(1, n).parallel()
: LongStream.range(1, n))
.reduce(1, (l, m) -> l * m);
}
public static void main(String[] args) {
System.out.println(factorial(10, true));
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得即使在单逻辑核心机器上,多线程减少仍然比单线程更快.如何使用流API对其进行测试或解决此问题?
我曾经写过这样的函数的替代品:
int[][] createMatrix(int height, int width) {
int[][] matrix = new int[height][width];
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
matrix[i][j] = i * width + j;
}
}
return matrix;
}
Run Code Online (Sandbox Code Playgroud)
但现在,我发现自己再也无法做到了.如何重构代码以在Java 8中使用流?
有人可以解释以下代码的行为吗?特别是为什么流中的forEach会更改原始列表?:
import java.util.ArrayList;
import java.util.List;
public class foreachIssue {
class justanInt {
public int anint;
public justanInt(int t){
anint=t;
}
}
public static void main(String[] args){
new foreachIssue();
}
public foreachIssue(){
System.out.println("The Stream Output:");
List<justanInt> lst = new ArrayList<>();
justanInt j1=new justanInt(2);
justanInt j2=new justanInt(5);
lst.add(j1);lst.add(j2);
lst.stream()
.map((s)->{
s.anint=s.anint*s.anint;
return s;
})
.forEach((s)->System.out.println("Anything"));
System.out.println(" lst after the stream:");
for(justanInt il:lst)
System.out.println(il.anint);
List<justanInt> lst1 = new ArrayList<>();
justanInt j3=new justanInt(2);
justanInt j4=new justanInt(5);
lst1.add(j3);lst1.add(j4);
lst1.stream()
.map((s)->{
s.anint=s.anint*s.anint;
return s;
});
System.out.println(" …Run Code Online (Sandbox Code Playgroud) 我是Java 8的新手,我正在研究流并遇到一个我无法解决的问题:
int[] intArr = new {1,2,4,6,7,8};
Run Code Online (Sandbox Code Playgroud)
我想使用Stream来获取数组中奇数位置的项目.
预期结果 : [1,4,7]
并使用Stream生成一个新的数组,该数组由以下形式组成: {intArr[i] + intArr[i+1]}
预期结果: [3,6,10,13,15]
我是JAVA编程的初学者.最近,我尝试使用s 的map&filter功能,Stream如下面的代码所示.
List<String> strList = Arrays.asList("abc", "", "bcd", "", "defg", "jk");
long count = strList.stream().filter(x -> x.isEmpty()).count();
System.out.printf("List %s has %d empty strings %n", strList, count);
Run Code Online (Sandbox Code Playgroud)
但是,编译器总是抱怨我需要"创建局部变量x".
也许我失去了一些基本步骤,这些步骤太容易在Google上找到任何相关的讨论.
我的JAVA编译器版本是J2SE 1.5,我将java.util.stream.*导入到我的文件中.
谢谢!
java functional-programming local-variables compiler-warnings java-stream
我创建了两个Stream
Stream<String> first= ...
Stream<String> second= ...
Run Code Online (Sandbox Code Playgroud)
他们俩都有数字.假设第一个文件有1到1000而第二个文件有25到35.我想检查第一个文件是否包含秒数的数字.
first.filter(s-> !s.contains(second.toString())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果我second.toString()用"10" 替换它然后它可以工作,但我如何检查整个流而不仅仅是字符串或字符串?
我们可以像这样从字符串List创建流
List<String> list = Arrays.asList("somestring");
Stream<String> stream = list.stream();
Run Code Online (Sandbox Code Playgroud)
但是如何直接从字符串创建流.
String x = "somestring";
Run Code Online (Sandbox Code Playgroud) 我上课了
public class Targets {
public int[] miniTargets;
public Targets(int t) {
miniTargets = new int[t];
for (int i = 0; i < t; i++) {
miniTargets[i] = t;
}
}
}
Run Code Online (Sandbox Code Playgroud)
而且我想要计算miniTargets我Targets收藏的数量,但我坚持flatMap:
public class App {
public static void main(String[] args) {
List<Targets> lst = new ArrayList<Targets>();
lst.add(new Targets(5));
lst.add(new Targets(15));
lst.add(new Targets(25));
int cnt = (int)lst.stream().map(tg -> tg.miniTargets).flatMap(...).count()
System.out.println(cnt);
}
}
Run Code Online (Sandbox Code Playgroud) String[] stringArray = streamString.toArray(size -> new String[size]);
Run Code Online (Sandbox Code Playgroud)
如何自动将大小视为流的大小?
如何使用流解析Generic到Object []?我有过
private static <T> List<Object[]> selectData(List<T> a,
Predicate<T> predicate) {
ArrayList<Object[]> tmp = new ArrayList<Object[]>();
for (T x : a) {
if (predicate.test(x)) {
tmp.add(new Object[] { x });
}
}
return tmp;
}
Run Code Online (Sandbox Code Playgroud)
但我想做的事情如下:
...//
return a.stream().filter(predicate).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将每个元素转换为Object []