Stream().distinct()

判断一个集合中是否有相同的元素

e1928a3d308d4fbc964eeb2e31c9cc2c.png

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java 开发者 配置jdk1.8 一下是jdk1.8的新特性Lamdbd表达式 forEach循环遍历 List<String> names = new ArrayList(); names.forEach(String x -> { System.out.println(x); System.out.println(x); }); Map<String,String> map = new HashMap(); map.forEach((x,y) -> System.out.println(x+":"+y)); 集合里放对象 List<Book> books; books.forEach(x -> System.out.println(x.getBookName())); List<Map> books = new ArrayList(); Map map = new HashMap(); map.put("name","aaa"); map.put("price",33); books.forEach(x -> x.forEach(y,z) -> System.out.println(y+":"+z)); 方法引用 names.forEach(System.out::println); 等同于 names.forEach(x->System.out.println(x)) removeIf 条件移除 List<Book> books; books.removeIf(x -> x.getBookPrice() > 22); stream 对集合进行处理,并不会改变集合,类似于数据库的select 创建stream 集合.stream() 操作stream filter 过滤 map 转换 skip 跳过 distinct 去重 limit 取几条 结果处理 foreach 输出 collect 收集 count 总条数 List<String> names = ; names.add("zhangsan"); names.add("zhangsan"); List nNews = names.stream.distinct().collect(Collectors.toList()); Collectors.toSet() //取出名字长度大于4的,跳过第1个,取2个 List<String> nNames = names.stream.filter(x->x.length()>4).skip(1),limt(2).collect(Collectors.toList()); //把长度转换成一个集合 List<Integer> c = names.stream.map(x->x.length()).collect(Collectors.toList()); List<Integer> c = names.stream.map(String::length).collect(Collectors.toList());

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值