zoukankan      html  css  js  c++  java
  • Collection接口下的Set接口TreeSet类中的自定义比较策略

    自定义比较策略的方法:

    1.先自定义一个比较器的类,在类中实现Comparator接口

    2.在类中重写compare()方法

    3.将自定义的比较器的一个实例作为构造方法的参数传入,则TreeSet就按照构造方法传入的比较器的比较策略对TreeSet的元素进行排序

    ========================================

    package cn.yunhe.collection.set;

    import java.util.Set;
    import java.util.TreeSet;
    import cn.yunhe.collection.util.ComparatorAce;
    import cn.yunhe.collection.util.ComparatorDsce;
    import cn.yunhe.collection.util.StudentList;

    public class TreeSet3 {
        public static void main(String[] args) {
            /**
             * 正序
             */
            Set<StudentList> tre = new TreeSet<StudentList>(new ComparatorAce());
            tre.add(new StudentList(1003, "tom"));
            tre.add(new StudentList(1001, "jack"));
            tre.add(new StudentList(1004, "lilie"));
            tre.add(new StudentList(1002, "james"));
            for (StudentList stu : tre) {
                System.out.println(stu.getId() + "," + stu.getName());
            }
            System.out.println("=============================");
            /**
             * 倒序
             */
            Set<StudentList> tre1 = new TreeSet<StudentList>(new ComparatorDsce());
            tre1.add(new StudentList(1003, "tom"));
            tre1.add(new StudentList(1001, "jack"));
            tre1.add(new StudentList(1004, "lilie"));
            tre1.add(new StudentList(1002, "james"));
            for (StudentList stu : tre1) {
                System.out.println(stu.getId() + "," + stu.getName());
            }

    ==========================================

    // 实现正序的比较器

    package cn.yunhe.collection.util;

    import java.util.Comparator;

    public  class ComparatorAce implements Comparator<StudentList>{

        public int compare( StudentList o1, StudentList o2) {
            if(o1.getId()>=o2.getId()){
                return 1;
            }else{
                return -1;
            }
            
        }

    }
    ============================================

    //实现倒序的比较器

    package cn.yunhe.collection.util;

    import java.util.Comparator;

    public class ComparatorDsce implements Comparator<StudentList> {

        @Override
        public int compare(StudentList o1, StudentList o2) {
            if (o1.getId() >= o2.getId()) {
                return -1;
            } else {
                return 1;
            }

        }

    }

    =============================
    //学生类

    package cn.yunhe.collection.util;

    public class StudentList {
        /**
         * 定义属性
         */
        private int id;
        private String name;

        /**
         * 构造方法
         */
        public StudentList() {
            super();
        }

        public StudentList(int id, String name) {
            super();
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

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

    }

  • 相关阅读:
    第三十一篇 玩转数据结构——并查集(Union Find)
    第三十篇 玩转数据结构——字典树(Trie)
    开发Electron可能用到的工具
    最新NetMonitor代码
    用C++/CLI搭建C++和C#之间的桥梁
    xaml实现无边框窗口
    用xaml画的带阴影3D感的圆球
    创作了一个xml的替代格式
    域名投资入门和技巧
    Compaq Visual Fortran生成静态库的方法及使用
  • 原文地址:https://www.cnblogs.com/pyobbiwitwsai/p/9520217.html
Copyright © 2011-2022 走看看