一、类定义
package com.xxx;
import java.math.BigDecimal;
public class Student {
private String name;
private Integer age;
private Boolean gender;
private Double score;
private BigDecimal money;
public Student() {
super();
}
public Student(String name, Integer age, Boolean gender, Double score, BigDecimal money) {
this.name = name;
this.age = age;
this.gender = gender;
this.score = score;
this.money = money;
}
@Override
public String toString() {
return "[name=" + this.name + ", age=" + this.age + ", gender=" + (gender ? "男" : "女") + ", score=" + this.score + ", money=" + this.money + "]";
}
public String getName() {
return name;
}
public Student setName(String name) {
this.name = name;
return this;
}
public Integer getAge() {
return age;
}
public Student setAge(Integer age) {
this.age = age;
return this;
}
public Boolean getGender() {
return gender;
}
public Student setGender(Boolean gender) {
this.gender = gender;
return this;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
}
二、工具类
package com.xxx;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DataHandler {
private static final DataHandler SINGLE = new DataHandler();
private DataHandler() {
super();
}
public static DataHandler getInstance() {
return SINGLE;
}
public <T> String convertList2String(List<T> list, String separator, String prefix, String suffix) {
String result = "";
if (list != null && list.size() > 0) {
result = prefix + list.get(0).toString() + suffix;
for (int index = 1; index < list.size(); index++) {
result = result + separator + prefix + list.get(index).toString() + suffix;
}
}
return result;
}
public <T1, T2> String convertMap2String(Map<T1, T2> map, String separator, String prefix, String suffix) {
String result = "";
if (map != null && map.size() > 0) {
Set<T1> keySet = map.keySet();
int index = 0;
for (T1 key : keySet) {
T2 value = map.get(key);
if (index == 0) {
result = prefix + key.toString() + ":" + value.toString() + suffix;
index++;
} else {
result = result + separator + prefix + key.toString() + ":" + value.toString() + suffix;
}
}
}
return result;
}
}
三、lambda表达式常见用法
package com.xxx;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test {
private static List<Student> studentList;
static {
studentList = new ArrayList<Student>();
studentList.add(new Student("WangShuang", 24, false, 91.5, new BigDecimal(30.01)));
studentList.add(new Student("ZhaoLiNa", 21, false, 65.0, new BigDecimal(10.02)));
studentList.add(new Student("WuLei", 26, true, 85.0, new BigDecimal(200.03)));
studentList.add(new Student("Aguero", 31, true, 60.0, new BigDecimal(7500.04)));
studentList.add(new Student("Silva", 34, true, 60.0, new BigDecimal(1500.05)));
studentList.add(new Student("Pogba", 26, true, 59.0, new BigDecimal(9000.06)));
}
private static DataHandler dataHandler = DataHandler.getInstance();
public static void main(String[] args) {
System.out.println("\n========== 0 ============================");
System.out.println(studentList.size());
System.out.println("\n========== 1 ============================");
List<String> nameList = studentList.stream().map(t -> t.getName()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(nameList, "、", "<", ">"));
List<Integer> ageList = studentList.stream().map(t -> t.getAge()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(ageList, "、", "<", ">"));
System.out.println("\n========== 2 ============================");
Map<String, Student> map1 = studentList.stream().collect(Collectors.toMap(Student::getName, t -> t));
System.out.println(dataHandler.convertMap2String(map1, "、", "<", ">"));
System.out.println("\n========== 3.1 ============================");
Integer ageSummation = studentList.stream().mapToInt(Student::getAge).sum();
System.out.println((24 + 21 + 26 + 31 + 34 + 26) + ", " + ageSummation);
System.out.println("\n========== 3.2 ============================");
BigDecimal moneySummation = studentList.stream().map(Student::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println((30.01 + 10.02 + 200.03 + 7500.04 + 1500.05 + 9000.06) + ", " + moneySummation);
System.out.println("\n========== 4 ============================");
List<Student> highScoreStudentList = studentList.stream().filter(t -> t.getScore() >= 60).collect(Collectors.toList());
List<String> highScoreNameList = highScoreStudentList.stream().map(t -> t.getName()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(highScoreNameList, "、", "<", ">"));
System.out.println("\n========== 5 ============================");
Map<Boolean, List<Student>> ageGroupMap = studentList.stream().collect(Collectors.groupingBy(Student::getGender));
System.out.println("男" + ageGroupMap.get(true).size() + "人, 女" + ageGroupMap.get(false).size() + "人");
System.out.println("\n========== 6 ============================");
studentList.sort(Comparator.comparing(Student::getScore));
List<String> ascSortNameList = studentList.stream().map(t -> t.getName()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(ascSortNameList, "、", "<", ">"));
List<Double> ascSortMoneyList = studentList.stream().map(t -> t.getScore()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(ascSortMoneyList, "、", "<", ">"));
System.out.println("\n========== 6.1 ============================");
studentList.add(new Student("XXX", 0, true, null, null));
studentList.sort(Comparator.comparing(Student::getScore, Comparator.nullsFirst(Comparator.naturalOrder())));
ascSortNameList = studentList.stream().map(t -> t.getName()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(ascSortNameList, "、", "<", ">"));
System.out.println("\n========== 6.2 ============================");
studentList.sort(Comparator.comparing(Student::getScore, Comparator.nullsLast(Comparator.naturalOrder())));
ascSortNameList = studentList.stream().map(t -> t.getName()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(ascSortNameList, "、", "<", ">"));
System.out.println("\n========== 7 ============================");
studentList.removeIf(t -> t.getScore() == null);
nameList = studentList.stream().map(t -> t.getName()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(nameList, "、", "<", ">"));
System.out.println("\n========== 8 ============================");
studentList.sort(Comparator.comparing(Student::getScore, Comparator.reverseOrder()));
List<String> descSortNameList = studentList.stream().map(t -> t.getName()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(descSortNameList, "、", "<", ">"));
List<Double> descSortMoneyList = studentList.stream().map(t -> t.getScore()).collect(Collectors.toList());
System.out.println(dataHandler.convertList2String(descSortMoneyList, "、", "<", ">"));
}
}