我想要做的是在下面的2个流调用中显示.我想根据某些条件将一个集合拆分为两个新集合.理想情况下,我想在1中进行.我已经看到了用于.map函数的条件,但找不到forEach的任何内容.实现我想要的最好方法是什么?
animalMap.entrySet().stream()
.filter(pair-> pair.getValue() != null)
.forEach(pair-> myMap.put(pair.getKey(), pair.getValue()));
animalMap.entrySet().stream()
.filter(pair-> pair.getValue() == null)
.forEach(pair-> myList.add(pair.getKey()));
Run Code Online (Sandbox Code Playgroud) 当流源是a时,我无法实现流处理的良好并行化Reader.在四核CPU上运行下面的代码我首先观察到3个核心,然后突然下降到两个核心,然后是一个核心.整体CPU利用率徘徊在50%左右.
请注意示例的以下特征:
这意味着所有压力都在CPU上,I/O很小.这个例子是一个用于自动并行化的坐鸭.
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
... class imports elided ...
public class Main
{
static final AtomicLong totalTime = new AtomicLong();
public static void main(String[] args) throws IOException {
final long start = System.nanoTime();
final Path inputPath = createInput();
System.out.println("Start processing");
try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(Paths.get("output.txt")))) {
Files.lines(inputPath).parallel().map(Main::processLine)
.forEach(w::println);
}
final double cpuTime = totalTime.get(),
realTime = System.nanoTime()-start;
final int cores = Runtime.getRuntime().availableProcessors();
System.out.println(" Cores: " + cores);
System.out.format(" CPU …Run Code Online (Sandbox Code Playgroud) 目前,我有这个方法,我想转换为Java 8流样式(我很少练习这个API btw,这就是这个小练习的目的):
private static Map<Integer, List<String>> splitByWords(List<String> list) {
for (int i = 0; i < list.size(); i++) {
if(list.get(i).length() > 30 && list.get(i).contains("-")) {
mapOfElements.put(i, Arrays.stream(list.get(i).split("-")).collect(Collectors.toList()));
} else if(list.get(i).length() > 30) {
mapOfElements.put(i, Arrays.asList(new String[]{list.get(i)}));
} else {
mapOfElements.put(i, Arrays.asList(new String[]{list.get(i) + "|"}));
}
}
return mapOfElements;
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所得到的:
private static Map<Integer, List<String>> splitByWords(List<String> list) {
Map<Integer, List<String>> mapOfElements = new HashMap<>();
IntStream.range(0, list.size())
.filter(i-> list.get(i).length() > 30 && list.get(i).contains("-"))
.boxed()
.map(i-> mapOfElements.put(i, Arrays.stream(list.get(i).split("-")).collect(Collectors.toList())));
//Copy/paste the above code …Run Code Online (Sandbox Code Playgroud) 我正在尝试Map根据条件逻辑和挣扎修改 a的键。我是 Java 8 流 API 的新手。假设我有一张这样的地图:
Map<String, String> map = new HashMap<>();
map.put("PLACEHOLDER", "some_data1");
map.put("Google", "some_data2");
map.put("Facebook", "some_data3");
map.put("Microsoft", "some_data4");
Run Code Online (Sandbox Code Playgroud)
当我想做的是PLACEHOLDER根据布尔条件找到引用并有条件地将该键更改为其他内容。我觉得它应该是什么样的下方,但这并不甚至编译过程的。
boolean condition = foo();
map = map.entrySet().stream().filter(entry -> "PLACEHOLDER".equals(entry.getKey()))
.map(key -> {
if (condition) {
return "Apple";
} else {
return "Netflix";
}
}).collect(Collectors.toMap(e -> e.getKey(), Map.Entry::getValue));
Run Code Online (Sandbox Code Playgroud)
我发现这个问题让我觉得也许我不能用 Java 8 流 API 来做到这一点。希望有比我更擅长的人知道如何做到这一点。如果你想玩它,Ideone 链接。