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

JDK1.8常用api整理

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

JDK1.8常用api整理

1.列表

1.1基础数据准备

    public static void main(String[] args) {
        List list1 = Arrays.asList(1, 3, 4, 5);
        List list2 = Arrays.asList(1, 3, 6, 8);
        List list3 = Arrays.asList(2, 5, 7, 9);
    }

1.2两个集合取并集

        List mergedList = Stream
                .concat(list1.stream(), list2.stream())
                .collect(Collectors.toList());

1.3两个集合取并集并去重

        List mergedList = Stream
                .concat(list1.stream(), list2.stream())
                .distinct()
                .collect(Collectors.toList());

1.4存在于集合1但是不存在与集合2中的数据

        List uniqueList1 = list1
                .stream()
                .filter(i -> !list2.contains(i))
                .collect(Collectors.toList());

1.5多个集合取并集

        List mergedList = Stream
                .of(list1, list2, list3)
                .flatMap(list -> list.stream())
                .collect(Collectors.toList());

1.6多个集合求和

        Integer sum = Stream
                .of(list1, list2, list3)
                .flatMap(list -> list.stream())
                .reduce(Integer::sum).orElse(0);

2.Map

2.1数据准备

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    
    private int grade;

    
    private int classID;

    
    private int studentID;

    
    private String name;

    
    private int age;

}
    public static void main(String[] args) {
        List studentList = new ArrayList<>();
        studentList.add(new Student(1, 1, 1, "小明", 6));
        studentList.add(new Student(1, 2, 2, "小红", 6));
        studentList.add(new Student(1, 2, 3, "小张", 7));
        studentList.add(new Student(2, 1, 4, "小王", 7));
        studentList.add(new Student(3, 1, 5, "小宋", 8));
    }

2.2以学生唯一标识(学号),将list转为map

        Map studentIdMap = studentList
                .stream()
                .collect(
                        Collectors.toMap(
                                student -> student.getStudentID(),
                                student -> student,
                                (curr, lastest) -> lastest)
                );

2.3以年级进行分组

        Map> gradeMap = studentList
                .stream()
                .collect(Collectors.groupingBy(Student::getGrade));

2.4以年级班级进行分组

        Map>> gradeClassMap = studentList
                .stream()
                .collect(Collectors.groupingBy(Student::getGrade,
                         Collectors.groupingBy(Student::getClassID)));

2.5获取每个年级年龄最小的同学,如果年龄相同,则取学号最小的同学

        Map gradeMinAgeMap = studentList
                .stream()
                .collect(Collectors.groupingBy(Student::getGrade,
                        Collectors.collectingAndThen(Collectors.toList(),
                                list -> list
                                        .stream()
                                        .sorted(Comparator.comparing(Student::getAge).thenComparing(Student::getStudentID))
                                        .findFirst()
                                        .orElse(null))));

3.多层Map

3.1数据准备

    public static void main(String[] args) {
        Map>>> mulitMap = new HashMap<>();
    }

3.2多层map的取值(不存在则创建相应的对象,存在则直接获取)

        List targetList = mulitMap
                .computeIfAbsent(1, key -> new HashMap<>())
                .computeIfAbsent(2, key -> new HashMap<>())
                .computeIfAbsent(3, key -> new ArrayList<>());

3.3多层map的取值并设置兜底数据

        List targetList = Optional.ofNullable(mulitMap)
                .map(map -> map.get(1))
                .map(map -> map.get(2))
                .map(map -> map.get(3))
                .orElse(new ArrayList<>());

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

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

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