image.png

    快捷键:一次性将相同变量名改名字: 选中要改的变量名shift + F6
    image.png

    1. package com.itheima.d2_params;
    2. import java.util.*;
    3. public class CollectionsDemo02 {
    4. public static void main(String[] args) {
    5. List<String> names = new ArrayList<>();
    6. // 这是正常添加集合元素
    7. // names.add("楚留香");
    8. // names.add("胡铁花");
    9. // names.add("张无忌");
    10. // names.add("陆小凤");
    11. // 1. 现在可以使用Collections的工具类的addAll API 即可实现一行代码添加全部元素
    12. Collections.addAll(names,"楚留香","胡铁花","张无忌","陆小凤"); // elements前面有三个点:表示可变参数
    13. System.out.println(names);
    14. // 2. public static void shuffie(List<?> list):打乱集合顺序
    15. Collections.shuffle(names); // 每次编译就会打乱一次顺序 Shuffle [ˈʃʌfl] 洗牌的意思
    16. System.out.println(names);
    17. // 3. public static <T> void sort(List<T> list): 将集合中元素按照默认排序规则排序 (排值特性的元素)
    18. List<Integer> list = new ArrayList<>();
    19. Collections.addAll(list,12,23,2,4);
    20. // list.add(23);
    21. // list.add(24);
    22. // list.add(12);
    23. // list.add(8);
    24. System.out.println(list); // 现在打印是没有排序的 [12, 23, 2, 4]
    25. Collections.sort(list); // 使用Collections工具类的sort Api可以排序
    26. System.out.println(list); // 排序成功了的 // [2, 4, 12, 23]
    27. // public static <T> void sort(List<T> list.Comparator<? super T> c);
    28. // 将集合中元素按照规则排序,自带比较器
    29. List<Apple> apples = new ArrayList<>(); // 可以重复!
    30. apples.add(new Apple("红富士","红色",9.9,500));
    31. apples.add(new Apple("青苹果","绿色",15.9,300));
    32. apples.add(new Apple("绿苹果","青色",29.9,400));
    33. apples.add(new Apple("黄苹果","红色",9.8, 500));
    34. // Collections.sort(apples); // 可以排序, 因为Apple类已经重写了比较规则(compareTo)
    35. // System.out.println(apples);
    36. // 方式二: sort方法自带比较器对象Comparator 其实方法是差不多的,都是定义比较器
    37. Collections.sort(apples, new Comparator<Apple>() {
    38. @Override
    39. public int compare(Apple o1, Apple o2) {
    40. return Double.compare(o1.getPrice() , o2.getPrice());
    41. }
    42. });
    43. System.out.println(apples);
    44. }
    45. }
    package com.itheima.d2_params;
    // 使用Apple类 去实现接口的某种功能  // Comparable这里是比较功能的接口,传入Apple对象
    public class Apple implements Comparable<Apple>{
        private String name;
        private String color;
        private double price;
        private int weight;
    
        public Apple() {
        }
    
        public Apple(String name, String color, double price, int weight) {
            this.name = name;
            this.color = color;
            this.price = price;
            this.weight = weight;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public int getWeight() {
            return weight;
        }
    
        public void setWeight(int weight) {
            this.weight = weight;
        }
        // 写javabean现在一般会再加一个toString方法(重写),如果有引用类型可以直接打印其内容
    
        @Override
        public String toString() {
            return "Apple{" +
                    "name='" + name + '\'' +
                    ", color='" + color + '\'' +
                    ", price=" + price +
                    ", weight=" + weight +
                    '}';
        }
    
        /**
         *  方式一: 类自定义比较规则
         * @param o the object to be compared.
         * @return
         */
        @Override
        public int compareTo(Apple o) {
            // 按照重量进行比较的
            // this代表你本身这个对象,o表示你放到参数里的对象  这里的this相当于o1 , o相当于o2
            // this是现在定义的这个类的学生对象,o是拿来比较输入的学生对象
            return this.weight - o.weight;  // 升序排序
    //        return this.weight - o.weight >= 0 ? 1 : -1; // 保留去重复的元素
        }
    }