自从JDK8之后,增加 Lambda表达式,用最少的代码,实现相同的功能,且流操作一定程度上提高代码执行效率。
学生对象:
import java.util.Objects;
public class Student implements Comparable<Student>{
private String id;
private String name;
private String age;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(id, student.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=‘" + id + ‘\‘‘ +
", name=‘" + name + ‘\‘‘ +
", age=‘" + age + ‘\‘‘ +
‘}‘;
}
@Override
public int compareTo(Student o) {
if(Integer.parseInt(this.getId()) > Integer.parseInt(o.getId())){
return 1;
}
if(this.getId().equals(o.getId())){
return 0;
}
return -1;
}
}
示例
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.lang.Integer.parseInt;
/**JDK 8 Lambda 表达式示例
*
*
* */
public class TestLambda {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
List<Integer> integerList = Arrays.asList(1,8,95,58,27);
Student student = new Student();
student.setId("101");
student.setName("周杰伦");
student.setAge("18");
studentList.add(student);
student = new Student();
student.setId("103");
student.setName("黄菲1");
student.setAge("221");
studentList.add(student);
student = new Student();
student.setId("102");
student.setName("林俊杰");
student.setAge("25");
studentList.add(student);
student = new Student();
student.setId("103");
student.setName("黄菲");
student.setAge("22");
studentList.add(student);
//1.遍历Lits<Object> 取出某一字段,形成列表
List<String> list = studentList.stream().map(s -> s.getName()).collect(Collectors.toList());
System.out.println("遍历Lits<Object> 取出某一字段,形成列表:" + list.toString());
//2.去重,需要重写对象的 equals 和 hashCode的方法,去重会对后面的数据进行去重,即A和B是一样的,后检查的会被去重
List<Student> list1 = studentList.stream().distinct().collect(Collectors.toList());
System.out.println("去重:" + list1.toString());
//3.过滤
List<Student> list2 = studentList.stream().filter(student1 -> {
if (Integer.parseInt(student1.getAge()) > 22){
return true;
}
return false;
}).collect(Collectors.toList());
System.out.println("过滤:" + list2.toString());
//4.统计
long count = studentList.stream().count();
System.out.println("统计:" + count);
//5.获取第一个对象
Optional<Student> student1 = studentList.stream().findFirst();
System.out.println("获取第一个对象:" + student1.get());
//6.全部匹配
Boolean result = studentList.stream().allMatch(student2 -> Integer.parseInt(student2.getAge()) > 25);
System.out.println("检测:" + result);
//7.存在一个检测
Boolean result1 = studentList.stream().anyMatch(student3 -> Integer.parseInt(student3.getAge()) > 25);
System.out.println("检测:" + result1);
//8.随机获取一个对象
Optional<Student> student2 = studentList.stream().findAny();
System.out.println("随机获取一个对象:" + student2.get());
//9.循环遍历
studentList.stream().forEach(student3 -> {
System.out.println(student3.toString());
});
//10. 排序 需要Student 继承 Comparable<Student>接口,实现 compareTo方法,即可排序,否则会抛出异常
List<Student> student4 = studentList.stream().sorted().collect(Collectors.toList());
System.out.println("排序:" + student4.toString());
//11. list 转 map
Map<String, Student> map = studentList.stream().distinct().collect(
Collectors.toMap(Student::getId, (s) -> s));
System.out.println("list 转 map:" + map);
//其他应用
Function<Student, Integer> function = (x) -> {
return parseInt(x.getAge()) ;
};
System.out.println("结果: " + function.apply(student));
}
}
遍历Lits<Object> 取出某一字段,形成列表
原文:https://www.cnblogs.com/fennudexiaofan/p/13356124.html