我试图找出所有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) 我想要List<InstanceWrapper>为每个元素做一些逻辑运算,从而得出一些结论String message。然后,我要进行create Map<String, String>,其中key是InstanceWrapper:ID,value是message;
private String handle(InstanceWrapper instance, String status) {
return "logic result...";
}
private Map<String, String> handleInstances(List<InstanceWrapper> instances, String status) {
return instances.stream().map(instance -> handle(instance, status))
.collect(Collectors.toMap(InstanceWrapper::getID, msg -> msg));
}
Run Code Online (Sandbox Code Playgroud)
但是它不会编译,我会明白,如何将stream().map()结果转化为collectors.toMap()价值?
The method collect(Collector<? super String,A,R>) in the type Stream<String> is not applicable for the arguments (Collector<InstanceWrapper,capture#5-of ?,Map<String,Object>>)
Run Code Online (Sandbox Code Playgroud) 我正在尝试根据条件过滤列表。是否有其他替代方法可以中断Java 8流以停止过滤?
举个例子:假设我有以下列表。
List<String> list = Arrays.asList("Foo","Food" ,"Fine","Far","Bar","Ford","Flower","Fire");
list.stream()
.filter(str -> str.startsWith("F")) //break when str doesn't start with F
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我希望从头开始以“ F”开头的所有字符串,一旦找到不以“ F”开头的字符串,我想停止过滤。没有流,我将执行以下操作:
List<String> result = new ArrayList<>();
for(String s : list){
if(s.startsWith("F")){
result.add(s);
}
else{
break;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在信息流中使用“中断”?
是否getSortedUsers()总是返回的orderd流getUsers() 独立的的getUsers()-collection类型?
public Set<User> getUsers(){
// unordered collection type
HashSet<User> set = new HashSet<>();
set.add(..);
set.add(..);
set.add(..);
return set;
}
// Is the stream only sorted if getUsers() is hold within a sortable collection type?
public Stream<User> getSortedUsers(Comparator<User> comp){
return getUsers().stream().sorted(comp);
}
Run Code Online (Sandbox Code Playgroud) 我将a HashMap<String, Double>转换为HashMap<String, Double>内容按值排序的位置。当我打印出以下内容时:
Stream<Map.Entry<String, Double>> sorted = map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
数据以正确的顺序打印出来,按值排序。但是,我不需要打印数据,我想将此流的内容折叠到具有新排序顺序的新HashMap中。我尝试了一些选择,但是当我这样做时,我似乎会找回原始的,未排序的HashMap:
return map.entrySet()
.stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));
Run Code Online (Sandbox Code Playgroud)
如何修改流,以便将已排序的HashMap发送回去?
我想计算整数列表的平均值,然后将结果四舍五入(即> = .5向上舍入为最接近的整数,而<.5向下舍入为最接近的整数)。如何使用Java流执行此操作?
public static void main(String [] args){
List<Integer> values = new ArrayList<>();
values.add(60);
values.add(30);
values.add(20);
double avg = values.stream().mapToInt(a -> a).average().getAsDouble();
System.out.println(avg);
}
Run Code Online (Sandbox Code Playgroud)
我希望有类似的东西
...average().round(RoundingMode.HALF_UP).getAsInt();
Run Code Online (Sandbox Code Playgroud) 我有一个EmpData对象
import java.util.ArrayList;
import java.util.List;
public class EmpData {
private List<Areas> areasList = new ArrayList<Areas>();
public List<Areas> getAreasList() {
return areasList;
}
public void setAreasList(List<Areas> areasList) {
this.areasList = areasList;
}
}
Run Code Online (Sandbox Code Playgroud)
和一个区域对象
public class Areas {
private String areaName;
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图遍历整个Area区域包含“ VENG”的empDataList,然后获取该特定的EmpData
这是我的测试客户
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ClientTest {
public static void main(String[] args) {
List<EmpData> empDataList = new …Run Code Online (Sandbox Code Playgroud) 我敢肯定有一个简单的方法可以做到这一点,但是我没有找到它。
本质上,我想做的是利用Java DoubleStream计算各种东西,例如值数组的平均值。但是,数组可能包含NaN值,这当然会导致结果也是NaN。因此,我想以一种方式,过滤掉NaN并计算平均值。
例如,此代码将产生NaN的结果,这是我不想要的。
import java.util.stream.DoubleStream;
public class StreamTests {
public static void main(String[] args) {
double[] x = {3.14159, 42, 2.71828, Double.NaN};
double mean = DoubleStream.of(x).average().getAsDouble();
System.out.println(mean);
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的:
import java.util.stream.DoubleStream;
public class StreamTests {
public static void main(String[] args) {
double[] x = {3.14159, 42, 2.71828, Double.NaN};
double mean = DoubleStream.of(x).ifNotNaN().average().getAsDouble();
System.out.println(mean);
}
}
Run Code Online (Sandbox Code Playgroud)
例如,我可以在Python中执行以下操作:
import math
x = [3.14159, 42, 2.71828, float('NaN')]
mean = sum([i for i in x if not math.isnan(i)]) / len(x)
print(mean)
Run Code Online (Sandbox Code Playgroud)
我知道为什么在第一个代码块中生成NaN,而只是标准的浮点行为。而且我也知道我可以循环遍历值并检查每个值,等等,但这正是我要避免的。我希望代码更加紧凑和优雅。
(import java.nio.file.Files)
(import java.nio.file.Paths)
(import java.util.stream.Stream)
(def path
(Paths/get "."
(into-array ["data" "10000000.test.log"])))
(def stream
(Files/lines path))
Run Code Online (Sandbox Code Playgroud)
这样我有:
stream
#object[java.util.stream.ReferencePipeline$Head 0x50129b8f
"java.util.stream.ReferencePipeline$Head@50129b8f"]
Run Code Online (Sandbox Code Playgroud)
有没有办法在不耗尽内存的情况下进行迭代?关于SO的建议并没有真正的帮助。文件约为1G。
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)