自己整理了一些常用的方法,废话不多说直接上代码
package hello;
import java.util.*;
import java.util.stream.Collectors;
/**
* Lamda表达式
*/
public class Lamda {
private static List<LamdaStudent> list;
public static void main(String[] args) {
list = Arrays.asList(
new LamdaStudent(7, "kobe"),
new LamdaStudent(1, "james"),
new LamdaStudent(4, "jordan"),
new LamdaStudent(6, "hans")
);
/**
* 1.Lamda表达式-Collections.sort-按照字段排序
*/
//LamdaCollectionSort();
/**
* 2.Lamda表达式-Comparator.comparing-按照字段排序
*/
//LamdaComparing();
/**
* 3.Lamda表达式-foreach-处理对象(数量不变)
*/
//LamdaForeach();
/**
* 4.Lamda表达式-stream.filter-处理对象(数量可能发生改变)类似findAll,findOne
*/
//LamdaStreamFilter();
/**
* 5.Lamda表达式-stream.filter.mapToInt-求和/平均值/最大/最小
*/
//LamdaStreamMapToInt();
/**
* 6.Lamda表达式-stream.filter.map-提取对象的属性到一个新的集合 类似Select
*/
LamdaStreamMap();
}
/**
* 1.Lamda表达式-Collections.sort-排序
*/
public static void LamdaCollectionSort() {
//id排序
Collections.sort(list, (a, b) -> {
return a.getId() - b.getId();
});
for (LamdaStudent lamdaStudent : list) {
System.out.println(lamdaStudent.toString());
}
}
/**
* 2.Lamda表达式-Comparator.comparing-排序
*/
public static void LamdaComparing() {
//正序(默认)由小到大
Collections.sort(list, Comparator.comparing(LamdaStudent::getId).thenComparing(LamdaStudent::getName));
System.out.println("正序");
for (LamdaStudent lamdaStudent : list) {
System.out.println(lamdaStudent.toString());
}
System.out.println("倒序");
//倒叙 由大到小
Collections.sort(list, Comparator.comparing(LamdaStudent::getId).reversed().thenComparing(LamdaStudent::getName));
for (LamdaStudent lamdaStudent : list) {
System.out.println(lamdaStudent.toString());
}
}
/**
* 3.Lamda表达式-foreach-处理对象(数量不变)
*/
public static void LamdaForeach() {
//处理对象(数量不变)
list.forEach(a -> {
//id处理
Integer id = a.getId();
if (id % 2 == 0) {
a.setId(id * 2);
}
});
for (LamdaStudent lamdaStudent : list) {
System.out.println(lamdaStudent.toString());
}
}
/**
* 4.Lamda表达式-stream().filter-处理对象(数量可能发生改变)类似findAll,findOne
*/
public static void LamdaStreamFilter() {
//返回结果集(类似findAll)
list = list.stream().filter(a -> (a.getId() > 1 && a.getName().length() >= 4)).collect(Collectors.toList());
System.out.println("返回结果集");
for (LamdaStudent lamdaStudent : list) {
System.out.println(lamdaStudent.toString());
}
//返回第一个对象(类似findOne)
LamdaStudent student = list.stream().filter(a -> (a.getId() > 1 && a.getName().length() >= 4)).findFirst().orElse(null);
System.out.println("返回第一个对象");
System.out.println(student.toString());
}
/**
* 5.Lamda表达式-stream().mapToInt-求和/平均值/最大/最小
*/
public static void LamdaStreamMapToInt() {
//求和
Integer sum = list.stream().mapToInt(a -> a.getId()).sum();
//平均值
Double avg = list.stream().mapToInt(a -> a.getId()).average().getAsDouble();
//最大
Integer max = list.stream().mapToInt(a -> a.getId()).max().getAsInt();
//最小
Integer min = list.stream().mapToInt(a -> a.getId()).min().getAsInt();
System.out.println("sum:" + sum);
System.out.println("avg:" + avg);
System.out.println("max:" + max);
System.out.println("min:" + min);
}
/**
* 6.Lamda表达式-stream().map-提取对象的属性到一个新的集合 类似Select
*/
public static void LamdaStreamMap() {
List<LamdaUser> userList = list.stream().map(a -> new LamdaUser(a.getId(), a.getName(), "admin123")).collect(Collectors.toList());
for (LamdaUser lamdaUser : userList) {
System.out.println(lamdaUser.toString());
}
}
}
/**
* 辅助类
*
* @author
*/
class LamdaStudent {
private int Id;
private String Name;
/**
* @param id
* @param name
*/
public LamdaStudent(int id, String name) {
Id = id;
Name = name;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
@Override
public String toString() {
return "LamdaStudent{" +
"Id=" + Id +
", Name='" + Name + '\'' +
'}';
}
}
/**
* 辅助类
*
* @author
*/
class LamdaUser {
private int uId;
private String uName;
private String uPwd;
/**
* @param id
* @param name
*/
public LamdaUser(int id, String name, String pwd) {
uId = id;
uName = name;
uPwd = pwd;
}
public int getuId() {
return uId;
}
public void setuId(int uId) {
this.uId = uId;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getuPwd() {
return uPwd;
}
public void setuPwd(String uPwd) {
this.uPwd = uPwd;
}
@Override
public String toString() {
return "LamdaUser{" +
"uId=" + uId +
", uName='" + uName + '\'' +
", uPwd='" + uPwd + '\'' +
'}';
}
}