接口Comparator的使用

  • 时间:
  • 来源:互联网
  • 文章标签:

接口Comparator的使用

​ > 这两天遇到比较和排序的场景,所以去学习了下Comparator的用法,在此做个记录

1. 排序

package com.ruoyi.system;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * @description: Comparator 接口的使用
 *      1.排序
 *
 * @author: zhanghailang
 * @date: 2020-10-22 13:22
 */
public class ComparatorTest {
    class Dog{
        public int age;
        public String name;

        public Dog(int age, String name) {
            this.age = age;
            this.name = name;
        }

        /**
         * 重写toString 方便输出
         * @return
         */
        @Override
        public String toString() {
            return "Dog [age = " + age + "] ,[name =" + name + "]";
        }
    }

    public static void main(String[] args) {
        ArrayList<Dog> list = new ArrayList<>();
        list.add(new ComparatorTest().new Dog(1,"Dog a"));
        list.add(new ComparatorTest().new Dog(2,"Dog b"));
        list.add(new ComparatorTest().new Dog(3,"Dog c"));

        //先按年龄进行排序
        Collections.sort(list, new Comparator<Dog>() {
            @Override
            public int compare(Dog o1, Dog o2) {
                return o1.age - o2.age;
            }
        });
        System.out.println("按照年龄来排序   :" + list);


        //再按姓名进行排序

        Collections.sort(list, new Comparator<Dog>() {
            @Override
            public int compare(Dog o1, Dog o2) {
                return o2.name.compareTo(o1.name);
            }
        });
        //字符串的排序和比较是根据unicode的排列来的(charAt())
        System.out.println("按照姓名来排序   :" + list);
        String test = "a";
        System.out.println((int)test.charAt(0));

    }
}
2.分组

package com.ruoyi.system;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

/**
 * @description: Comparator  接口的使用
 *         2.分组
 * @author: zhanghailang
 * @date: 2020-10-22 14:28
 */
public class ComparatorTest2 {

    class Apple{
        public String color;
        public int weight;

        public Apple(String color, int weight) {
            this.color = color;
            this.weight = weight;
        }

        @Override
        public String toString() {
            return "Apple [color=" + color + ", weight=" + weight + "]";
        }
    }

    /**
     * <? super T> : 下界通配符
     * <? extend T> : 上界通配符
     * 泛型相关讲解:https://www.zhihu.com/question/20400700
     * @param datas
     * @param c
     * @param <T>
     * @return
     */
    private static <T> List<List<T>> divider(Collection<T> datas, Comparator<? super T> c){
        List<List<T>> result = new ArrayList<>();
        for (T t : datas){
            boolean isSameGroup = false;
            for (int j = 0; j < result.size(); j++){
                if(c.compare(t,result.get(j).get(0)) == 0){
                    isSameGroup = true;
                    result.get(j).add(t);
                    break;
                }
            }

            if (!isSameGroup){
                List<T> innerList = new ArrayList<>();
                result.add(innerList);
                innerList.add(t);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        ArrayList<Apple> list = new ArrayList<>();
        list.add(new ComparatorTest2().new Apple("粉",233));
        list.add(new ComparatorTest2().new Apple("红",222));
        list.add(new ComparatorTest2().new Apple("红",222));
        list.add(new ComparatorTest2().new Apple("粉",233));
        list.add(new ComparatorTest2().new Apple("绿",210));
        list.add(new ComparatorTest2().new Apple("绿",210));
        //按颜色进行分组
        List<List<Apple>> byColors = divider(list, new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.color.compareTo(o2.color);
            }
        });
        System.out.println("Orde by Colors: "+ byColors);

    }
}

stay hungry

本文链接http://www.taodudu.cc/news/show-1782066.html