定义一个测试对象
public class UserBean {
/**
* 用户id
*/
private Integer userId;
/**
* 用户名称
*/
private String name;
/**
* 性别
*/
private String sex;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public UserBean(Integer userId, String name, String sex) {
this.userId = userId;
this.name = name;
this.sex = sex;
}
public UserBean() {
}
}
一、截取字段
public class Test {
public static void main(String[] args) {
List<UserBean> list = new ArrayList<>();
list.add(new UserBean(111,"张三","男"));
list.add(new UserBean(444,"赵六","女"));
list.add(new UserBean(222,"李四","男"));
list.add(new UserBean(333,"王五","女"));
System.out.println("原数据:"+JsonUtil.toJson(list));
List<Integer> userBeans = list.stream().map(UserBean::getUserId).collect(Collectors.toList());
System.out.println("执行后:"+JsonUtil.toJson(userBeans));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6b15341fbc9447a3d325d9c5ab06f0e3.png)
二、筛选字段
public class Test {
public static void main(String[] args) {
List<UserBean> list = new ArrayList<>();
list.add(new UserBean(111,"张三","男"));
list.add(new UserBean(444,"赵六","女"));
list.add(new UserBean(222,"李四","男"));
list.add(new UserBean(333,"王五","女"));
System.out.println("原数据:"+JsonUtil.toJson(list));
List<UserBean> userBeans = list.stream().filter(userBean -> userBean.getSex().equals("男")).collect(Collectors.toList());
System.out.println("用户id:"+JsonUtil.toJson(userBeans));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/77e10a3bfa7afad4f1faa8ce67b5dca0.png)
三、读取前几条数据
public class Test {
public static void main(String[] args) {
List<UserBean> list = new ArrayList<>();
list.add(new UserBean(111,"张三","男"));
list.add(new UserBean(444,"赵六","女"));
list.add(new UserBean(222,"李四","男"));
list.add(new UserBean(333,"王五","女"));
System.out.println("原数据:"+JsonUtil.toJson(list));
List<UserBean> userBeans = list.stream().limit(2).collect(Collectors.toList());
System.out.println("用户id:"+JsonUtil.toJson(userBeans));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/04077dc4415c72950fbe5f9c64cc754b.png)
四、跳过前面数据
public class Test {
public static void main(String[] args) {
List<UserBean> list = new ArrayList<>();
list.add(new UserBean(111,"张三","男"));
list.add(new UserBean(444,"赵六","女"));
list.add(new UserBean(222,"李四","男"));
list.add(new UserBean(333,"王五","女"));
System.out.println("原数据:"+JsonUtil.toJson(list));
List<UserBean> userBeans = list.stream().skip(1).limit(2).collect(Collectors.toList());
System.out.println("用户id:"+JsonUtil.toJson(userBeans));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/77e7a7acb67af44a97d4485edc083c20.png)
五、去重
对象里面需要重写equals 和 hashCode
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserBean userBean = (UserBean) o;
return Objects.equals(userId, userBean.userId) && Objects.equals(name, userBean.name) && Objects.equals(sex, userBean.sex);
}
@Override
public int hashCode() {
return Objects.hash(userId, name, sex);
}
public class Test {
public static void main(String[] args) {
List<UserBean> list = new ArrayList<>();
list.add(new UserBean(111,"张三","男"));
list.add(new UserBean(444,"赵六","女"));
list.add(new UserBean(222,"李四","男"));
list.add(new UserBean(333,"王五","女"));
list.add(new UserBean(333,"王五","女"));
System.out.println("原数据:"+JsonUtil.toJson(list));
List<UserBean> userBeans = list.stream().distinct().collect(Collectors.toList());
System.out.println("用户id:"+JsonUtil.toJson(userBeans));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/782c262a0f80f45c39efa85d91dd23b7.png)
六、排序
1:顺序
public class Test {
public static void main(String[] args) {
List<UserBean> list = new ArrayList<>();
list.add(new UserBean(111,"张三","男"));
list.add(new UserBean(444,"赵六","女"));
list.add(new UserBean(222,"李四","男"));
list.add(new UserBean(333,"王五","女"));
System.out.println("原数据:"+JsonUtil.toJson(list));
List<UserBean> userBeans = list.stream().sorted(Comparator.comparingInt(UserBean::getUserId)).collect(Collectors.toList());
System.out.println("执行后:"+JsonUtil.toJson(userBeans));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/d0be155669099fe0093c13330af010d9.png)
2:倒序
public class Test {
public static void main(String[] args) {
List<UserBean> list = new ArrayList<>();
list.add(new UserBean(111,"张三","男"));
list.add(new UserBean(444,"赵六","女"));
list.add(new UserBean(222,"李四","男"));
list.add(new UserBean(333,"王五","女"));
System.out.println("原数据:"+JsonUtil.toJson(list));
List<UserBean> userBeans = list.stream().sorted(Comparator.comparingInt(UserBean::getUserId).reversed()).collect(Collectors.toList());
System.out.println("执行后:"+JsonUtil.toJson(userBeans));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/0678e9625f6f60fb8873be027c6d3995.png)
学无止境,其他由你们去扩充吧~