• List 集合的一些常用操作


    import java.util.ArrayList;
    import java.util.List;
    
    class Student {
    
        private String name;
        private Integer age;
    
        public Student(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Perosn [name=" + name + ", age=" + age + "]";
        }
    }
    
    public class Test {
        public static void main(String[] args) {
    
            List<Student> students = new ArrayList<>();
            students.add(new Student("abc", 12));
            students.add(new Student("bcd", 20));
            students.add(new Student("cde", 17));
            students.add(new Student("def", 25));
            students.add(new Student("efg", 15));
            
            for (Student stu : students) {
                System.out.println(stu);
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在集合尾部添加元素

    Student student = new Student("123", 12);
    int endOfList = students.size();
    students.add(endOfList, student);
    
    Student [name=abc, age=12]
    Student [name=bcd, age=20]
    Student [name=cde, age=17]
    Student [name=def, age=25]
    Student [name=efg, age=15]
    Student [name=123, age=12]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    根据某个字段进行排序

    students.sort((x, y) -> Integer.compare(x.getAge(), y.getAge()));
    
    Student [name=abc, age=12]
    Student [name=123, age=12]
    Student [name=efg, age=15]
    Student [name=cde, age=17]
    Student [name=bcd, age=20]
    Student [name=def, age=25]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    根据某个属性去重

    import static java.util.stream.Collectors.collectingAndThen;
    import static java.util.stream.Collectors.toCollection;
    import static java.util.Comparator.comparingLong;
    
    List<Student> list = students.stream().collect(
        collectingAndThen(
            toCollection(() -> new TreeSet<>(comparingLong(Student::getAge))), ArrayList::new)
    );
    
    Student [name=abc, age=12]
    Student [name=efg, age=15]
    Student [name=cde, age=17]
    Student [name=bcd, age=20]
    Student [name=def, age=25]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    软考高级信息系统项目管理师系列论文九:论信息系统项目的采购管理
    分享几个关于Camera的坑
    【算法面试题汇总】LeetBook列表的算法面试题汇总---树题目及答案
    SpringBoot业务开发 05、SpringBoot集成JSR303实现参数校验+全局异常捕捉
    sudo和su的区别及使用
    计组与操作系统
    6 种在 JavaScript 中将字符串转换为数组的方法
    LeetCode | 1851.包含每个查询的最小区间
    SQL binary 轉float 絕對好用
    力扣232 - 用栈实现队列【C语言实现】
  • 原文地址:https://blog.csdn.net/weixin_44129618/article/details/126189191