使用java8 api方法list.stream().map().collect(Collectors.toList())

使用java8 api方法list.stream().map().collect(Collectors.toList())

userList User实体类对象集合
User 实体类
getId 实体类属性的get方法
List<int> ids= userList.stream().map(User::getId).collect(Collectors.toList())

把数据放到map根据user.getId(条件) 循环 在转换成list

List<int> ids= userList.stream().map(user->user.getId()).collect(Collectors.toList());

过滤list集合中属性type为1的值并赋值给permissions集合 在返回list集合 .collect(Collectors.toList()) 转换成list集合

List<Permission> permissions = list.stream().filter(l -> l.getType().equals(1))
                .collect(Collectors.toList());

java api 特性

stream只能被“消费”一次,一旦遍历过就会失效,就像容器的迭代器那样,想要再次遍历必须重新生成
map():用于映射每个元素到对应的结果。 filter():filter 方法用于通过设置的条件过滤出元素。
Collectors.toList() 用来结束Stream流

例如:

//userList User实体类对象集合
//User 实体类
//getId 实体类属性的get方法 
List<int> ids= userList.stream().map(User::getId).collect(Collectors.toList())

//或者 把数据放到map根据user.getId(条件) 循环 在转换成list

List<int> ids= userList.stream().map(user->user.getId()).collect(Collectors.toList());

//过滤list集合中属性type为1的值并赋值给permissions集合 在返回list集合 .collect(Collectors.toList()) 转换成list集合

List<Permission> permissions = list.stream().filter(l -> l.getType().equals(1))
                .collect(Collectors.toList());

list转map

Map<String,Entity> statMap = statList.stream().collect(Collectors.toMap(Entity::getId, Entity -> Entity));
        
List<String> collect = roleResultList.stream().map(AcAppRole::getName).collect(Collectors.toList());
Map<Integer, String> 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<Integer> result = map.keySet().stream().collect(Collectors.toList());
result.forEach(System.out::println);
System.out.println("\n2. Export Map Value to List...");
List<String> 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<String> result3 = map.keySet().stream().filter(x -> !"banana".equalsIgnoreCase(x)).collect(Collectors.toList());
result3.forEach(System.out::println);
 
List<String> resultValues = map.entrySet().stream().sorted(Map.Entry.<Integer, String>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<SysRole> roleList = sysRoleService.list(roleWrapper);
List<String> roleIdList = roleList.stream().map(SysRole::getId).collect(Collectors.toList());
List<String> roleNames = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
sysUser.setUserRoleNames(CollectionUtil.join(roleNames, ","));

字符串转list

List<Long> shipIdList = Arrays.stream(shipIds.split(",")).map(Long::parseLong).collect(Collectors.toList());

list根据某个对象属性去重

// 根据name去重

List<Person> unique = persons.stream().collect(
            Collectors.collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new)
);

// 根据name,sex两个属性去重

List<Person> unique = persons.stream().collect(
           Collectors. collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
);
List<Person> filterList = persons.stream().filter(p -> p.getSex().equals(1)).collect(Collectors.toList());
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值