Java/Rust stream流
Java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Stream {
public static void main(String[] args) {
ArrayList<student> students = new ArrayList<>();
students.add(new student(1, "lzw"));
students.add(new student(1, "lzw"));
students.add(new student(2, "lzww"));
students.add(new student(3, "lzwww"));
students.add(new student(4, "lzwwww"));
students.add(new student(5, "lzwwwww"));
System.out.println(students.stream().filter(x -> x.getId() > 3).collect(Collectors.toList()));
System.out.println(students.stream().distinct().collect(Collectors.toList()));
students.stream().forEach(System.out::println);
System.out.println(students.stream().map(s -> {
s.setId(s.getId() + 1);
return s;
}).collect(Collectors.toList()));
}
}
Rust
fn main() {
#[derive(Debug,Clone)]
struct student{
name: String,
id: u64,
}
let mut students:Vec<student> = vec![];
for i in 1..5{
students.push(student {
name: "lzw".to_string(),
id: i,
});
}
//map
let x = students.clone().into_iter().map(|mut x1| {
x1.id += 1;
x1
}).collect::<Vec<_>>();
println!("{:?}",x);
//filter
let vec1 = students.into_iter().filter(| x2| {
if x2.id == 2 {
true
}else {
false
}
}).collect::<Vec<_>>();
println!("{:?}",vec1);
}
|