Java 在不调用最终函数forEach()的情况下执行中间函数
我的代码读取Java 在不调用最终函数forEach()的情况下执行中间函数,java,lambda,Java,Lambda,我的代码读取people.txt,构造许多person对象并将它们添加到persons列表中。 但是如果我删除forEach,stream.map()函数不会执行,因为它是一个中间函数,因此我需要forEach或任何其他最终函数 但是,我不希望在forEach函数(本例中为print语句)中执行任何使用者lambda。我该怎么做 我的主要方法是: public static void main(String[] args) { List<Person> persons=ne
people.txt
,构造许多person对象并将它们添加到persons列表中。
但是如果我删除forEach,stream.map()函数不会执行,因为它是一个中间函数,因此我需要forEach或任何其他最终函数
但是,我不希望在forEach函数(本例中为print语句)中执行任何使用者lambda。我该怎么做
我的主要方法是:
public static void main(String[] args) {
List<Person> persons=new ArrayList<>();
try(
BufferedReader reader=
new BufferedReader(
new InputStreamReader(
Main.class.getResourceAsStream("people.txt")));
Stream<String> stream=reader.lines();
){
stream.map(line->{
String[] s=line.split(" ");
Person p=new Person(
s[0].trim(),
Integer.parseInt(s[1]),
s[2].trim()
);
persons.add(p);
return p;
}).forEach(System.out::print);
}catch(IOException e){
System.out.println(e);
}
publicstaticvoidmain(字符串[]args){
List persons=new ArrayList();
试一试(
缓冲读取器=
新的缓冲读取器(
新的InputStreamReader(
getResourceAsStream(“people.txt”);
Stream=reader.lines();
){
stream.map(行->{
字符串[]s=line.split(“”);
人员p=新人员(
s[0]。修剪(),
整数.parseInt(s[1]),
s[2].trim()
);
增加(p);
返回p;
}).forEach(系统输出::打印);
}捕获(IOE异常){
系统输出打印ln(e);
}
如果您不打算收集任何东西,则无需绘制地图。
通过将它们添加到列表中,您已经在使用它们,因此您可以执行以下操作:
stream.forEach(line->{
String[] s=line.split(" ");
Person p=new Person(
s[0].trim(),
Integer.parseInt(s[1]),
s[2].trim()
);
persons.add(p);
})
这样,您仍然可以填写列表。
另外,你能做的是
stream.map(line->{
String[] s=line.split(" ");
Person p=new Person(
s[0].trim(),
Integer.parseInt(s[1]),
s[2].trim()
);
return p;
}).forEach(persons::add);
此外,您不应该在map函数中执行任何副作用,即向列表中添加内容是。map()应仅用于将内容映射到流,向列表中添加内容是使用它的方式。如果这是您向人员列表中添加内容的唯一位置,您还可以执行以下操作:
persons = stream.map(line->{
String[] s=line.split(" ");
Person p=new Person(
s[0].trim(),
Integer.parseInt(s[1]),
s[2].trim()
);
return p;
}).collect(Collectors.toList());
List persons=stream.map(第->{
字符串[]s=line.split(“”);
人员p=新人员(
s[0]。修剪(),
整数.parseInt(s[1]),
s[2].trim()
}).collect(Collectors.toList());
如果要使用自定义列表而不是
.collect(Collectors.toCollection(persons))
使用唯一流API的更紧凑的解决方案:
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
Main.class.getResourceAsStream("people.txt")))) {
List<Person> peoples = reader.lines()
.map(line -> line.split(" "))
.map(tuple -> new Person(
tuple[0].trim(),
Integer.parseInt(tuple[1]),
tuple[2].trim()
))
.collect(Collectors.toList());
peoples.forEach(System.out::println);
} catch (IOException e) {
System.out.println(e);
}
try(BufferedReader reader=new BufferedReader(new InputStreamReader(
Main.class.getResourceAsStream(“people.txt”)){
List people=reader.lines()
.map(直线->直线分割(“”)
.map(元组->新人物(
元组[0]。修剪(),
整数.parseInt(元组[1]),
元组[2].trim()
))
.collect(Collectors.toList());
peoples.forEach(System.out::println);
}捕获(IOE异常){
系统输出打印ln(e);
}
流API不适用于具有副作用的函数。您的map lambda会将内容作为副作用添加到列表中。下面的答案显示了map的使用方式
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
Main.class.getResourceAsStream("people.txt")))) {
List<Person> peoples = reader.lines()
.map(line -> line.split(" "))
.map(tuple -> new Person(
tuple[0].trim(),
Integer.parseInt(tuple[1]),
tuple[2].trim()
))
.collect(Collectors.toList());
peoples.forEach(System.out::println);
} catch (IOException e) {
System.out.println(e);
}