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

list.stream().map().collect(Collectors.toList())

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

list.stream().map().collect(Collectors.toList())

java api 特性
stream只能被“消费”一次,一旦遍历过就会失效,就像容器的迭代器那样,想要再次遍历必须重新生成
map():用于映射每个元素到对应的结果。
filter():filter 方法用于通过设置的条件过滤出元素。
Collectors.toList() 用来结束Stream流
例如:
//userList User实体类对象集合
//User 实体类
//getId 实体类属性的get方法 
List ids= userList.stream().map(User::getId).collect(Collectors.toList())
//或者  把数据放到map根据user.getId(条件) 循环 在转换成list
List ids= userList.stream().map(user->user.getId()).collect(Collectors.toList());

//过滤list集合中属性type为1的值并赋值给permissions集合 在返回list集合 .collect(Collectors.toList()) 转换成list集合
List permissions = list.stream().filter(l -> l.getType().equals(1))
                .collect(Collectors.toList());
 

list转map

Map statMap = statList.stream().collect(Collectors.toMap(Entity::getId, Entity -> Entity));
        
List collect = roleResultList.stream().map(AcAppRole::getName).collect(Collectors.toList());
Map map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
System.out.println("n1. Export Map Key to List...");
List result = map.keySet().stream().collect(Collectors.toList());
result.forEach(System.out::println);
System.out.println("n2. Export Map Value to List...");
List result2 = map.values().stream().collect(Collectors.toList());
result2.forEach(System.out::println);
System.out.println("n3. Export Map Value to List..., say no to banana");
List result3 = map.keySet().stream().filter(x -> !"banana".equalsIgnoreCase(x)).collect(Collectors.toList());
result3.forEach(System.out::println);
 
List resultValues = map.entrySet().stream().sorted(Map.Entry.comparingByKey().reversed())
                .peek(e -> resultSortedKey.add(e.getKey()))
                .map(x -> x.getValue())
                .filter(x -> !"banana".equalsIgnoreCase(x))
                .collect(Collectors.toList());
 
    public static void main(String args[]) {
        SqlServerReader tester = new SqlServerReader();
        tester.testCaseFormat();
    }
 
    private void testCaseFormat() {
        System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));
        System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));
 
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));
        System.out.println();
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));
    }
 

list转逗号分隔的字符串CollectionUtil.join

List roleList = sysRoleService.list(roleWrapper);
List roleIdList = roleList.stream().map(SysRole::getId).collect(Collectors.toList());
List roleNames = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
sysUser.setUserRoleNames(CollectionUtil.join(roleNames, ","));

字符串转list

List shipIdList = Arrays.stream(shipIds.split(",")).map(Long::parseLong).collect(Collectors.toList());
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/716587.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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