java8的一些新特性使用,废话不多说直接上代码。
定义一个对象,省略get、set以及构造方法
public class Student {
private String name;
private int age;
private long id;
}
import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<Student> students = getStudents();
List<Student> students2 = getStudents2();
System.out.println("年龄最大的学生 : "+getMaxAgeStudent(students));
System.out.println("ID最小的学生 : "+getminIdStudent(students));
System.out.println("是否存在年龄为23岁的学生 : "+anymatch(students));
System.out.println("是否存所有学生的年龄都小于30岁 : "+allmatch(students));
System.out.println("是否存所有学生的年龄都小于30岁 : "+noneMatch(students));
System.out.println("年龄大于23岁的学生数量 : "+getCount(students));
System.out.println("年龄大于23岁的学生列表 : "+getStudents(students));
System.out.println("按年龄,id倒序排序 : "+getStudentsByIdDesc(students));
System.out.println("按年龄,id正序排序 : "+getStudentsByIdAsc(students));
System.out.println("学生名字拼接 : "+getJoinStudentName(students));
System.out.println("拼装map : "+getStudentMap(students));
System.out.println("name集合 : "+nameList(students));
System.out.println("年龄平均值 : "+ageAvg(students));
System.out.println("年龄和 : "+sumAvg(students));
System.out.println("根据true或false进行分区 : "+listMap(students));
System.out.println("根据年龄分组 : "+listMapByAge(students));
System.out.println("重新赋值 : "+reSetStudent(students,students2));
}
public static List<Student> getStudents(){
List<Student> students = new ArrayList<>();
Student s1 = new Student("张三",22,1);
Student s2 = new Student("李四",23,2);
Student s3 = new Student("王五",22,3);
Student s4 = new Student("麻六",25,4);
Student s5 = new Student("赵七",26,5);
Student s6 = new Student("赵七",26,7);
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
students.add(s6);
return students;
}
public static List<Student> getStudents2(){
List<Student> students = new ArrayList<>();
Student s1 = new Student("张三",25,1);
Student s2 = new Student("李四",26,2);
students.add(s1);
students.add(s2);
return students;
}
//年龄最大的学生
public static Student getMaxAgeStudent(List<Student> students){
Student result = students.stream().max(Comparator.comparing(student -> student.getAge())).get();
return result; //年龄最大的学生 : Student{name='赵七', age=26, id=5}
}
//ID最小的学生
public static Student getminIdStudent(List<Student> students){
Student result = students.stream().min(Comparator.comparing(student -> student.getId())).get();
return result; //ID最小的学生 : Student{name='张三', age=22, id=1}
}
//判断是否存在年龄为23的学生 anyMatch表示,判断的条件里,任意一个元素成功,返回true
public static boolean anymatch(List<Student> students){
boolean flag = students.stream().anyMatch(student -> student.getAge() == 23);
return flag;
}
//判断是否存所有学生的年龄都小于30岁 allMatch表示,判断条件里的元素,所有的都是,返回true
public static boolean allmatch(List<Student> students){
boolean flag = students.stream().allMatch(student -> student.getAge() < 30);
return flag;
}
//判断是否存所有学生的年龄都小于30岁 noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
public static boolean noneMatch(List<Student> students){
boolean flag = students.stream().noneMatch(student -> student.getAge() > 30);
return flag;
}
//过滤年龄大于23岁的学生数量
public static long getCount(List<Student> students){
long count = students.stream().filter(student -> student.getAge() > 23).count();
return count;
}
//过滤年龄大于23岁的学生列表
public static List<Student> getStudents(List<Student> students){
List<Student> studentList = students.stream().filter(student -> student.getAge() > 23).collect(Collectors.toList());
return studentList;
}
//按年龄,ID倒序排列
public static List<Student> getStudentsByIdDesc(List<Student> students){
students.sort(Comparator.comparing(Student::getAge).thenComparing(Student::getId).reversed());
return students;
}
//按年龄,id正序排列
public static List<Student> getStudentsByIdAsc(List<Student> students){
students.sort(Comparator.comparing(Student::getAge).thenComparing(Student::getId));
return students;
}
//所有学生的名字拼接
public static String getJoinStudentName(List<Student> students){
String names = students.stream().map(student -> student.getName()).collect(Collectors.joining(","));
return names;
}
//把学生拼装成Map
public static Map<Long,String> getStudentMap(List<Student> students){
Map<Long, String> collect = students.stream().collect(Collectors.toMap(Student::getId, Student::getName));
for (Object o : collect.keySet()) {
System.out.println("key=" + o + " value=" + collect.get(o));
}
System.out.println(collect.get(1L));
return collect;
}
//所有学生的名字列表
public static List<String> nameList(List<Student> students){
List<String> collect = students.stream().map(Student::getName).collect(Collectors.toList());
return collect;
}
//计算学生年龄平均值
public static Double ageAvg(List<Student> students){
Double collect = students.stream().collect(Collectors.averagingInt(Student::getAge));
return collect;
}
//计算学生年龄和
public static Integer sumAvg(List<Student> students){
Integer collect = students.stream().collect(Collectors.summingInt(Student::getAge));
return collect;
}
//根据true或false进行分区 年龄大于23的在true集合,小于23的在false集合
public static Map<Boolean,List<Student>> listMap(List<Student> students){
Map<Boolean,List<Student>> listMap = students.stream().collect(Collectors.partitioningBy(e -> e.getAge() > 23));
return listMap;
}
//根据年龄分组
public static Map<Integer, List<Student>> listMapByAge(List<Student> students){
Map<Integer, List<Student>> integerListMap = students.stream().collect(Collectors.groupingBy(Student::getAge));
return integerListMap;
}
//重新赋值
public static List<Student> reSetStudent(List<Student> students ,List<Student> students2){
students.stream().forEach(student ->{
Optional<Student> first = students2.stream().filter(student2 -> student.getAge() == student2.getAge()).findFirst();
if (first.isPresent()){
student.setName(first.get().getName());
}
} );
return students;
}
}