java8 stream best practice

all contains

Posted by laosuan on July 2, 2020

1 拼接集合中的字符串

String allStr = courseList.stream().map(e -> e.getTitle()).collect(Collectors.joining());

2 集合中数字求和

double totalDuration = courseList.stream().mapToDouble(d -> Double.valueOf(d.getDuration())).sum();

3 集合中的字段构造成数组

List<Integer> questionIdList = questionList.stream().map(Question::getId).collect(Collectors.toList());

4 filter过滤&集合中的字段构造成map数组

List<Map<String, Object> remoteMapData = new ArrayList<>();

List<Map<String, Object>> finalRemoteData = remoteMapData.stream().filter(r -> !r.get("user_id").equals(CategoryType.ADMIN_DOWN_ID)).map(r -> {
                Map<String, Object> mapR = new HashMap<>();
                mapR.put("portId", r.get("_id"));
                mapR.put("portAuthor", r.get("user"));
                return mapR;
            }).collect(Collectors.toList());

5 构造指定值的数组并遍历

Stream.of("1", "2", "3").forEach(System.out::println);