栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA笔记 | 集合类及JAVA8运用的例子笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA笔记 | 集合类及JAVA8运用的例子笔记

1.根据对象集合中的对象的某个属性排序,再根据第二个属性排序

运用场景:按不同组排序,输出集合导出excel;

数据源

List tests = Arrays.asList(new TestVO("A","1"),new TestVO("A","4"),new TestVO("A","6"), new TestVO("C","1"), new TestVO("A","2"),new TestVO("B","2"),
new TestVO("B","1"));

List tests2 = tests.stream().sorted(Comparator.comparing(TestVO::getValue).thenComparing(TestVO::getType)).collect(Collectors.toList());

2.如上排序后,需要取每一个组的前几个数据

 Map> tests3 = tests.stream().sorted(
               Comparator.comparing(TestVO::getValue)
                       .thenComparing(TestVO::getType))
               .collect(Collectors.groupingBy(TestVO::getType,linkedHashMap::new,Collectors.toList()));
       List result = new ArrayList<>();
       tests3.forEach((v , k) ->{
           result.addAll(k.stream().limit(3).collect(Collectors.toList()));
       });
        result.stream().forEach(System.out::print);

3.Map获取键值对的几种方式

Map maps = new HashMap<>();
        maps.put("1","苹果");
        maps.put("2","菠萝");
        maps.put("3","西瓜");
        //0.直接获取键值内容
        maps.forEach((k,v) ->{
            System.out.print(k+v);
        });
        //1.keySet获取所有键
        maps.keySet().forEach(e ->{
            System.out.print(maps.get(e));
        });
        //2.values获取所有值
        maps.values().forEach(System.out::print);
        //3.获取所有键值对组
        maps.entrySet().forEach(e ->{
            System.out.print(e.getKey() + e.getValue());
        });

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号