分享几个JDK8新特性中常用的几个实用方法

1.集合元素去重
List strList = new ArrayList<>();
strList.add(“aa”);
strList.add(“aa”);
strList.add(“bb”);
strList.add(“cc”);
strList.add(“cc”);
strList.add(“dd”);
strList.add(“dd”);
/元素去重/
//方式一
Set mySet = new LinkedHashSet<>();
mySet.addAll(strList);在这里插入代码片
System.out.println(mySet);
strList.clear();
strList.addAll(mySet);
System.out.println(strList);
//方式二
List collect = strList.stream().distinct().collect(Collectors.toList());
很明显,方式二显得很简洁明了。
2.操作集合元素
Student s1 = new Student(2,“zhangsan”,“bj”,12);
Student s2 = new Student(2,“zhangsan”,“bj”,12);
Student s3 = new Student(3,“jack”,“bj”,12);
Student s4 = new Student(3,“tom”,“bj”,12);
Student s5 = new Student(4,“jack”,“bj”,12);
Student s6 = new Student(4,“tom”,“tj”,12);
Student s7 = new Student(5,“wangwu”,“bj”,12);
Student s8 = new Student(5,“lisi”,“bj”,12);
List students = Arrays.asList(s1,s2,s3,s4,s5,s6,s7,s8);
Random rd = new Random();
students = students.stream().map(student -> {
student.setId(rd.nextInt(10));
student.setName(UUID.randomUUID().toString().replaceAll(“-”,“”));
student.setAge(rd.nextInt(30));
student.setAddress(“xxx”);
return student;
}).collect(Collectors.toList());
System.out.println(students);
3.集合元素过滤
Student s1 = new Student(2,“zhangsan”,“bj”,12);
Student s2 = new Student(2,“zhangsan”,“bj”,12);
Student s3 = new Student(3,“jack”,“bj”,12);
Student s4 = new Student(3,“tom”,“bj”,12);
Student s5 = new Student(4,“jack”,“bj”,12);
Student s6 = new Student(4,“tom”,“tj”,12);
Student s7 = new Student(5,“wangwu”,“bj”,12);
Student s8 = new Student(5,“lisi”,“bj”,12);
List students = Arrays.asList(s1,s2,s3,s4,s5,s6,s7,s8);
students = students.stream().distinct().collect(Collectors.toList());
students = students.stream().parallel().filter(s-> (“jack”.equals(s.getName()) || “tj”.equals(s.getAddress()))).collect(Collectors.toList());
System.out.println(students);
4.集合取差集
List a1 = new ArrayList<>();
a1.add(“aaa”);
List a2 = new ArrayList<>();
a2.add(“aaa”);
a2.add(“bbb”);
a2.add(“ccc”);
//取a2 与 a1的差集
a2 = a2.stream().filter(s ->!a1.contains(s)).collect(Collectors.toList());
System.out.println(a2);
5.集合元素分组
People people1 = new People(1, “only-qi1”, “11”, 11);
People people2 = new People(2, “only-qi2”, “12”, 12);
People people3 = new People(3, “only-qi3”, “14”, 13);
People people4 = new People(1, “only-qi4”, “14”, 14);
People people5 = new People(2, “only-qi5”, “13”, 15);
People people6 = new People(1, “only-qi6”, “11”, 16);
ArrayList arrayList = new ArrayList<>();
arrayList.add(people1);
arrayList.add(people2);
arrayList.add(people3);
arrayList.add(people4);
arrayList.add(people5);
arrayList.add(people6);
Map<String, List> group = arrayList.stream().collect(Collectors.groupingBy(People::getAddress));
System.out.println(group);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值