我试图找出所有ForkJoinPool线程何时完成其任务。我写了这个测试应用程序(我使用System.out是因为它只是一个快速的测试应用程序,并且没有错误检查/处理):
public class TestForkJoinPoolEnd {
private static final Queue<String> queue = new LinkedList<>();
private static final int MAX_SIZE = 5000;
private static final int SPEED_UP = 100;
public static void main(String[] args) {
ForkJoinPool customThreadPool = new ForkJoinPool(12);
customThreadPool.submit(
() -> makeList()
.parallelStream()
.forEach(TestForkJoinPoolEnd::process));
enqueue("Theard pool started up");
int counter = MAX_SIZE + 1;
while (!customThreadPool.isTerminating()) {
String s = dequeue();
if (s != null) {
System.out.println(s);
counter--;
}
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
}
}
System.out.println("counter …Run Code Online (Sandbox Code Playgroud) 编译器如何确保以下语句的等效lambda
BinaryOperator<String> concatOperator = String::concat;
Run Code Online (Sandbox Code Playgroud)
是
BinaryOperator<String> concatOperator = (resultString, inputString) -> resultString.concat(inputString);
Run Code Online (Sandbox Code Playgroud)
并不是
BinaryOperator<String> concatOperator = (resultString, inputString) -> inputString.concat(resultString);
Run Code Online (Sandbox Code Playgroud) I have a function like this:
public static Xyz getXyz(P p) {
if (p == null) {
return null;
}
List<Object> bs = p.getB();
if (CollectionUtils.isEmpty(Bs)) {
return null;
}
for (Object b : bs) {
if (b instanceof R) {
R r = (R) b;
List<Object> cObjects = r.getB();
for (Object cObject : cObjects) {
if (cObject instanceof C) {
C c = (C) cObject;
Object vObject = cObject.getV();
if (vObject instanceof V) {
return r.getXyz();
}
}
} …Run Code Online (Sandbox Code Playgroud) 我试图在Integer流上使用reduce方法来获取所有值的总和。但出现语法错误,无法找出错误。
List<Integer> ee = new ArrayList<Integer>();
Function<? super Integer, ? extends Integer> f3 = x -> x / 2;
BinaryOperator<? extends Integer> accumulator = (x, y) -> x + y;
ee.stream().map(f3).reduce(new Integer(0), accumulator);
Run Code Online (Sandbox Code Playgroud)
它给出了错误:
The method reduce(capture#2-of ? extends Integer, BinaryOperator<capture#2-of ? extends Integer>) in the type Stream<capture#2-of ? extends Integer> is not applicable for the arguments (Integer, BinaryOperator<capture#7-of ? extends Integer>)
Run Code Online (Sandbox Code Playgroud) 我有简单对象的列表:
private String unit;
private Double value;
Run Code Online (Sandbox Code Playgroud)
列表看起来像这样:
f, 1.0;
ml, 15.0;
g, 9.0
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的函数,希望将这些值分组并以unit为键将它们放入地图,并将对象列表作为值,但是我想像在原始列表中那样保存顺序。这是我目前的解决方案:
myList.stream()
.collect(groupingBy(MyObject::getUnit));
Run Code Online (Sandbox Code Playgroud)
但是之后,我的地图按字母顺序排序:f,g,ml,而不是f,ml,g。有没有其他分组方法可以解决?
我正在尝试将玩家列表转换为地图。播放器包含名称并作为变量运行。
List<Player> runnerList = Arrays.asList(new Player("Virat", 4654), new Player("Jaddu", 5798),
new Player("Dhoni", 4581), new Player("Virat", 8709), new Player("Dhoni", 4711),
new Player("Virat", 4541));
Run Code Online (Sandbox Code Playgroud)
我的问题是我试图通过使用流合并运行来转换为地图,而无法通过。
尝试每个并合并值,如下所示,并获得预期结果。
playerList.forEach(n -> {
mapVal.merge((n.getName()), (n.getDistance()), (val1, val2) -> IntStream.of(val1, val2).sum());
});
Run Code Online (Sandbox Code Playgroud)
寻找使用流的解决方案的结果将是{Dhoni = 9292,Jaddu = 5798,Virat = 17904}。
JPMS 模块名称中不允许使用破折号。据我了解,建议用点替换它们。
但是,我找不到带有替换建议的 Java 模块名称中其他不允许的字符的列表。例如,是否允许使用“_”字符?
我正在研究 Java8 中 Stream 和 Lambda 的用法。您将如何将 Stream 和 Lambda 应用于以下内容:使用 Stream 和 Lambda 打印出以下字符串中的奇数:“[3, 6, 8, 96, 7, 23]”。
我发现使用 for 循环和 parseInt 非常简单,但是如何实现流和 lambda。
我一直在 HackerRank 做一些练习测试,并在某个时候决定只使用流来解决它(作为个人挑战)。我做到了。程序工作一般。但是,当涉及到大量数据需要遍历时,程序需要很长时间才能完成。正因为如此,最终我没有解决测试,因为“因超时而终止:(”。我完全同意。当我在自己的 PC 上运行这个程序时,不仅需要很长时间才能完成,而且我的工作期间CPU温度暴涨...
这是我创建的代码:
List<Integer> duplicatesCount = arr.stream()
.map(x -> Collections.frequency(arr, x))
.collect(Collectors.toList());
OptionalInt maxDuplicate = duplicatesCount.stream().mapToInt(Integer::intValue).max();
Set<Integer> duplicates = arr.stream()
.filter(x -> Collections.frequency(arr, x) == maxDuplicate.getAsInt())
.collect(Collectors.toSet());
OptionalInt result = duplicates.stream().mapToInt(Integer::intValue).min();
return result.getAsInt();
Run Code Online (Sandbox Code Playgroud)
谁可以给我解释一下这个?流通常会给 CPU 带来如此大的压力吗?还是只是这个程序?
附注。我上面提到的数据(这个程序无法处理的数据)有 73966 位从 1 到 5 的数字。如果这很重要或有人感兴趣......
我在 MacOS 上使用 Java 14 中的 JPackage,需要在构建 DMG 之前将文件复制到应用程序的 MacOS 和资源文件夹,但不知道如何操作。在打包工具用户指南中,它提到从名为 application-name-post-image.sh 的资源文件夹运行脚本,但这似乎没有发生。
java ×10
java-stream ×7
java-8 ×4
collectors ×1
cpu ×1
cpu-usage ×1
deployment ×1
for-loop ×1
grouping ×1
hashmap ×1
java-14 ×1
java-9 ×1
java-module ×1
jpackage ×1
lambda ×1
list ×1
loops ×1
packing ×1
string ×1