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

1年前 (2023) 程序员胖胖胖虎阿
117 0 0

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());

版权声明:程序员胖胖胖虎阿 发表于 2023年3月21日 下午6:08。
转载请注明:list.stream().map().collect(Collectors.toList()) | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...