失效链接处理 |
JDK8新特性_Stream PDF 下载
本站整理下载:
提取码:r8nz
相关截图:
主要内容:
1、创建实体类
2、创建测试类
//创建实体类 @Data public class User { //姓名 private String name; //年龄 private Integer age; //性别 private Integer sex; //地址 private String address; //赏金 private BigDecimal money; public User(String name, Integer age, Integer sex, String address,BigDecimal money) { this.name = name; this.age = age; this.sex = sex; this.address = address; this.money = money; }@Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", sex=" + sex + ", money=" + money + ", address='" + address + '\'' + '}'; } }public class Stream { public static void main(String[] args) { }public static List<User> users(){ List<User> list = Arrays.asList( new User("李星云", 18, 0, "渝州",new BigDecimal(1000)), new User("陆林轩", 16, 1, "渝州",new BigDecimal(500)),
new User("姬如雪", 17, 1, "幻音坊",new BigDecimal(800)), new User("袁天罡", 99, 0, "藏兵谷",new BigDecimal(100000)), new User("张子凡", 19, 0, "天师府",new BigDecimal(900)), new User("陆佑劫", 45, 0, "不良人",new BigDecimal(600)), new User("张天师", 48, 0, "天师府",new BigDecimal(1100)), new User("蚩梦", 18, 1, "万毒窟",new BigDecimal(800)) );return list; }/*filter过滤(T-> boolean)*/ public static void filter(){ List<User> list = users(); List<User> newlist = list.stream().filter(user -> user.getAge() > 20) .collect(Collectors.toList()); for (User user : newlist) { System.out.println(user.getName()+" --> "+ user.getAge()); } }---结果--- 袁天罡 --> 99 陆佑劫 --> 45 张天师 --> 48 /*distinct 去重*/ 数据源中复制new User("李星云", 18, 0, "渝州",new BigDecimal(1000)) 并粘贴两个 public static void distinct(){ List<User> list = users(); List<User> newlist = list.stream().distinct().collect(Collectors.toList()); for (User user : newlist) { System.out.println(user.getName()+" --> "+ user.getAge()); } }---结果--- 李星云 --> 18 陆林轩 --> 16 姬如雪 --> 17 袁天罡 --> 99 张子凡 --> 19 陆佑劫 --> 45 张天师 --> 48 蚩梦 --> 18 /*sorted排序*/ public static void sorted(){ List<User> list = users(); List<User> newlist = list.stream() .sorted(Comparator.comparingInt(User::getAge)) .collect(Collectors.toList()); for (User user : newlist) { System.out.println(user.getName()+" --> "+ user.getAge()); } }---结果---
陆林轩 --> 16 姬如雪 --> 17 李星云 --> 18 蚩梦 --> 18 张子凡 --> 19 陆佑劫 --> 45 张天师 --> 48 袁天罡 --> 99 /*limit返回前n个元素*/ public static void limit(){ List<User> list = users(); List<User> newlist = list.stream() .sorted(Comparator.comparingInt(User::getAge)) .limit(2) .collect(Collectors.toList()); for (User user : newlist) { System.out.println(user.getName()+" --> "+ user.getAge()); } }---结果--- 陆林轩 --> 16 姬如雪 --> 17 /*skip去除前n个元素*/ public static void skip(){ List<User> list = users(); List<User> newlist = list.stream() .sorted(Comparator.comparingInt(User::getAge)) .skip(2) .collect(Collectors.toList()); for (User user : newlist) { System.out.println(user.getName()+" --> "+ user.getAge()); } }---结果--- 李星云 --> 18 蚩梦 --> 18 张子凡 --> 19 陆佑劫 --> 45 张天师 --> 48 袁天罡 --> 99 /*map(T->R)*/ public static void map(){ List<User> list = users(); List<String> newlist = list.stream() .map(User::getName).distinct().collect(Collectors.toList()); for (String add : newlist) { System.out.println(add); } }---结果
|