欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Collections工具类

程序员文章站 2022-03-01 15:01:02
...

Collections常用方法:

                      1.public static <T> boolean addAll(Collection<T> c,T...elements)          往集合中添加一些元素

public class Demo02 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,1,3,2,4,5);
        System.out.println(list);
    }
}
输出的就是[1, 3, 2, 4, 5]

                      2.public static void shuffle(List<?> list)                打乱集合顺序

public class Demo02 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,1,3,2,4,5);
        Collections.shuffle(list);
        System.out.println(list);
    }
}
输出的就是它们打乱之后的样子。比如[4, 1, 2, 3, 5]

                      3.public static <T> void sort(List<T> list)                将集合中的元素按照默认(升序)规则排序

public class Demo02 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,1,3,2,4,5);
        Collections.shuffle(list);
        Collections.sort(list);
        System.out.println(list);
    }
}
输出的就是[1, 2, 3, 4, 5]

                      4.public static <T> void sort(List<T> list,Comparator<? super T>)       将集合中元素按照指定规则排序

Comparable  和    Comparator比较器:

第一种方法:     继承Comparable<Student>接口

定义一个Student学生类:

public class Student implements Comparable<Student> {
    private String name;
    private int age;
    private float score;

    public Student(String name, int age, float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Float.compare(student.score, score) == 0 &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age, score);
    }

    @Override
    public int compareTo(Student o) {
        if (this.score > o.score) {
            return -1;
        } else if (this.score < o.score) {
            return 1;
        }else{
            if (this.age > o.age) {
                return 1;
            } else if (this.age < o.age) {
                return -1;
            } else {
                return 0;
            }
        }
    }
}
注意:成绩是按照降序排列的,年龄是升序排列的

定义一个实现类:

public class Test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("liu", 20, 90.0F);
        Student stu2 = new Student("zhang", 22, 90.0F);
        Student stu3 = new Student("guan", 20, 99.0F);
        Student stu4 = new Student("ma", 22, 100.0F);
        Collections.addAll(list, stu1, stu2, stu3, stu4);
        Collections.sort(list);
        for (Student s : list) {
            System.out.println(s);
        }
    }
}
结果为
      Student{name='ma', age=22, score=100.0}
      Student{name='guan', age=20, score=99.0}
      Student{name='liu', age=20, score=90.0}
      Student{name='zhang', age=22, score=90.0}

第二种方法:   使用Comparator比较器

1.定义一个学生类:

public class Student {
    private String name;
    private int age;
    private float score;

    public Student(String name, int age, float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }
}

定义一个实现类

public class Test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("liu", 20, 90.0F));
        list.add(new Student("zhang", 22, 90.0F));
        list.add(new Student("liu", 20, 99.0F));
        list.add(new Student("liu", 22, 100.0F));
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getScore() > o2.getScore()) {
                        return -1;
                } else if (o1.getScore() < o2.getScore()){
                    return 1;
                }else{
                    if (o1.getAge() > o2.getAge()) {
                        return 1;
                    } else if (o1.getAge() < o2.getAge()) {
                        return -1;
                    }else{
                        return 0;
                    }
                }
            }
        });
        for (Student s : list) {
            System.out.println(s);
        }
    }
}
中间那个为比较器
输出结果为
          Student{name='liu', age=22, score=100.0}
          Student{name='liu', age=20, score=99.0}
          Student{name='liu', age=20, score=90.0}
          Student{name='zhang', age=22, score=90.0}

 

上一篇: Spfa模板

下一篇: SPFA模板