共 14 篇文章 |
|
java中List根据某一字段去重。当一个List中存在某一个字段或者几个一样的时候就把这样的数据作为重复数据,需要去掉重复数据。1. 根据某一个字段去重,也可以用多个字段来去重。List<Entity> arrays = arr.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()-> new TreeSet<>(Comparator.comparing(Ent... 阅357 转2 评0 公众公开 22-09-13 11:43 |
}/** * Returns an {@code Optional} with the specified present non-null value. * * @param <T> the class of the value * @param value the value to be present, which must be non-null * @return an {@code Optional} with the value present * @throws NullPointerException if value is null */public static <T> Optiona... 阅3 转0 评0 公众公开 22-04-16 10:59 |
在Java8的foreach()中使用return/break/continue,不会跳出循环。想知道这是为什么,在Stack Overflow中找到一个答案,主要是说foreach()不是一个循环,不是设计为可以用break以及continue来中止的操作。1、foreach()循环操作元素时,是否会退出lambda表达式,如下图所示:不管你遍历到哪个集合中的元素,上图都会停在第一行程序中而不会发生跳... 阅20 转0 评0 公众公开 21-07-09 18:19 |
接下来我们将会使用java 8 的流式sorted排序List 、Map 、 Set 1、sorted() 默认使用自然序排序, 其中的元素必须实现Comparable 接口 2、sorted(Comparator<? super T> comparator) :我们可以使用lambada 来创建一个Comparator 实例。slist = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collect... 阅445 转0 评0 公众公开 21-04-28 09:27 |
java8 toMap(key重复如何解决)使用stream的toMap()函数时,当key重复,系统会报错相同的key不能形成一个map,那么需要解决这个问题,一:相同key的情况下,丢弃重复的只保留一个,二:相同key的情况下,把value变成list,形成Map(Object,List<Object>)的形式。1:当key重复,使用最后一个value为值:2:当key重复,使用list收集: 阅292 转0 评0 公众公开 20-08-25 17:03 |
Java Stream流之求和。BigDecimal:BigDecimal bb =list.stream().map(Plan::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);int、double、long:double max = list.stream().mapToDouble(User::getHeight).sum();productVideoMap.put(k, v.stream().mapToLong(e -> cn.hutool.core.date.DateUtil.between(e.getStartTime(), e.getEndTi... 阅1964 转0 评0 公众公开 20-07-02 15:30 |
【转】【Lambda】java8笔记: sorted()之正序倒序。list.stream().sorted(Comparator.comparing(Student::getAge))list.stream().sorted(Comparator.comparing(Student::getAge).reversed())list.sort(Comparator.comparing(Integer::intValue).reversed());list.sort(Comparator.comparing(Student::getAge));list.sort(Comparator.comparing(S... 阅1571 转0 评0 公众公开 20-04-22 21:46 |
Java8 教程第五章之Streams Collectors groupingBy.public static void main(String[] args) { //3 apple, 2 banana, others 1 List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya");//group by Salary Map&... 阅131 转0 评0 公众公开 20-03-28 20:14 |
JAVA8之妙用Optional解决判断Null为空的问题引言。public final class Optional<T> { //省略.... private static final Optional<?> EMPTY = new Optional<>(); private Optional() { this.value = null; } //省略... public static<T> Optional<T> empty() { @SuppressWarnings("unchecked") Optiona... 阅82 转0 评0 公众公开 20-02-27 22:37 |
打印结果:元素的属性值倒序:[{name=''''''''李四'''''''', salary=68.0}, {name=''''''''张三'''''''', salary=50.0}, {name=''''''''王五'''''... 阅1297 转0 评0 公众公开 20-01-02 16:39 |