Java8部分特性及list的常见操作
2022/1/7 22:03:48
本文主要是介绍Java8部分特性及list的常见操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- Java8部分特性及list的常见操作
public class SkuList { public static void main(String[] args) { List<User> students = new ArrayList<>(); User st1 = new User(); st1.setId(123); st1.setAge(10); User st2 = new User(); st2.setId(789); st2.setAge(20); User st3 = new User(); st3.setId(456); st3.setAge(20); students.add(st1); students.add(st2); students.add(st3); /* TODO: 输出:list分割:[[{"age":10,"id":123},{"age":20,"id":123}],[{"age":20,"id":456}]] List<List<User>> splits = Lists.partition(students, 2); System.out.println("list分割:" + JSON.toJSONString(splits));*/ /* TODO: 输出:list根据年龄进行分组:{20:[{"age":20,"id":123},{"age":20,"id":456}],10:[{"age":10,"id":123}]} Map<Integer, List<User>> listMap = students.stream().collect(Collectors.groupingBy(User::getAge)); System.out.println("list根据年龄进行分组:" + JSON.toJSONString(listMap));*/ /* TODO: 输出:list转换为Map:{789:{"age":20,"id":789},456:{"age":20,"id":456},123:{"age":10,"id":123}} Map<Integer, User> userMap = students.stream().collect(Collectors.toMap(User::getId, Function.identity())); System.out.println("list转换为Map:" + JSON.toJSONString(userMap));*/ /* TODO: 输出:list过滤filter :[{"age":20,"id":789},{"age":20,"id":456}] List<User> oldStu = students.stream().filter( student -> student.getAge() > 10).collect(Collectors.toList()); System.out.println("list过滤filter :" + JSON.toJSONString(oldStu));*/ /* TODO: 输出:list对象转化:[{"age":10,"id":123,"title":"教师"},{"age":20,"id":789,"title":"教师"},{"age":20,"id":456,"title":"教师"}] List<Teacher> teachers = students.stream().map( student -> { Teacher teacher = new Teacher(); BeanUtils.copyProperties(student, teacher); teacher.setTitle("教师"); return teacher; }).collect(Collectors.toList()); System.out.println("list对象转化:" + JSON.toJSONString(teachers));*/ /* TODO: 输出:list转set:[20, 10] Set<Integer> ages = students.stream().map(User::getAge).collect(Collectors.toSet()); System.out.println("list转set:" + ages);*/ /* TODO: 输出:[奔驰, 宝马, 奥迪] List<Customer> customerList = new ArrayList<>(); customerList.add(new Customer(1L, "奔驰")); customerList.add(new Customer(2L, "宝马")); customerList.add(new Customer(3L, null)); customerList.add(null); customerList.add(new Customer(5L, null)); customerList.add(new Customer(6L, "奥迪")); List<String> nameList1 = customerList.stream() .filter(Objects::nonNull) // 過濾掉Customer為null的元素 .map(Customer::getName) .filter(Objects::nonNull) // 過濾掉Customer.getName()為null的元素 .collect(Collectors.toList()); System.out.println(nameList1);*/ } } @Data class User { private Integer id; private Integer age; } /** * 转换使用 */ @Data class Teacher { private Integer id; private Integer age; private String title; } @Data @AllArgsConstructor class Customer { private Long id; private String name; }
这篇关于Java8部分特性及list的常见操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25Java编程面试题详解与解答
- 2024-12-25TS基础知识详解:初学者必看教程
- 2024-12-252024面试题解析与攻略:从零开始的面试准备指南
- 2024-12-25数据结构与算法学习:新手入门教程
- 2024-12-25初学者必备:订单系统资料详解与实操教程
- 2024-12-24内网穿透资料入门教程
- 2024-12-24微服务资料入门指南
- 2024-12-24微信支付系统资料入门教程
- 2024-12-24微信支付资料详解:新手入门指南
- 2024-12-24Hbase资料:新手入门教程