zoukankan      html  css  js  c++  java
  • Java 中HashTable、HashMap、TreeMap三者区别,以及自定义对象是否相同比较,自定义排序等

    /*
    Map集合:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
    Map
    	|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。效率低。基本已废弃
    	|--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,.效率高,不保证顺序。
    	|--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的键进行排序。保证顺序
    */
    
    import java.util.*;
    
    /**
     * 学生类实现Comparable可比较接口
     */
    class Student implements Comparable<Student> {
        private String name;
        private int age;
    
        Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        /**
         * 覆写接口中的compareTo方法跟equals覆写组合判断对象是否相同
         *
         * @param s
         * @return
         */
        @Override
        public int compareTo(Student s) {
            int num = Integer.valueOf(this.age).compareTo(s.age);
            if (num == 0)
                return this.name.compareTo(s.name);
            return num;
        }
    
        /**
         * 覆写hashcode方法用于底层采用hash算法的容器
         *
         * @return
         */
        @Override
        public int hashCode() {
            //用姓名和年龄组合值作为hashcode
            return name.hashCode() + age * 34;
        }
    
        /**
         * 覆写equals方法实现自定义比较
         *
         * @param obj
         * @return
         */
        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof Student))
                throw new ClassCastException("类型不匹配");
            //强制转换类
            Student s = (Student) obj;
            //比较类中成员变量是否相同
            return this.name.equals(s.name) && this.age == s.age;
        }
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    
        public String toString() {
            return name + "," + age;
        }
    }
    
    /**
     * 比较器
     */
    class StuNameComparator implements Comparator<Student> {
        public int compare(Student s1, Student s2) {
            int num = s1.getName().compareTo(s2.getName());
            if (num == 0)
                return Integer.valueOf(s1.getAge()).compareTo(s2.getAge());
            return num;
        }
    }
    
    class Test2 {
        public static void main(String[] args) {
            HashMap<Student, String> hm = new HashMap<Student, String>();
    
            hm.put(new Student("lisi1", 21), "beijing");
            hm.put(new Student("lisi1", 21), "beijing"); //与上面相同不存
            hm.put(new Student("lisi1", 21), "tianjin");
            hm.put(new Student("lisi2", 22), "shanghai");
            hm.put(new Student("lisi3", 23), "nanjing");
            hm.put(new Student("lisi4", 24), "wuhan");
            hm.put(new Student("lisi4", 24), "wuhan"); //与上面相同不存
    
            System.out.println("-----------------第第一种取出方式 keySet, 迭代器-------------------------");
            //第一种取出方式 keySet, 迭代器
    
            Set<Student> keySet = hm.keySet();
    
            Iterator<Student> it = keySet.iterator();
    
            while (it.hasNext()) {
                Student stu = it.next();
                String addr = hm.get(stu);
                System.out.println(stu + "___" + addr);
            }
    
            System.out.println("-----------------第二种取出方式 entrySet, 迭代器-------------------------");
    
            //第二种取出方式 entrySet, 迭代器
            Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
    
            Iterator<Map.Entry<Student, String>> iter = entrySet.iterator();
    
            while (iter.hasNext()) {
                Map.Entry<Student, String> me = iter.next();
                Student stu = me.getKey();
                String addr = me.getValue();
                System.out.println(stu + "___" + addr);
            }
    
            System.out.println("---------------第三种取出方式 for循环方式---------------------------");
    
            //第三种取出方式 for循环方式
            for (Map.Entry<Student, String> entry : hm.entrySet()) {
                System.out.println(entry.getKey() + "___" + entry.getValue());
            }
    
            System.out.println("----------------TreeMap自定义排序,使用比较器--------------------------");
            //TreeMap自定义排序,使用比较器
            TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
            tm.put(new Student("lisi1", 21), "beijing");
            tm.put(new Student("lisi1", 21), "beijing"); //与上面相同不存
            tm.put(new Student("lisi1", 21), "tianjin");
            tm.put(new Student("lisi2", 22), "shanghai");
            tm.put(new Student("lisi3", 23), "nanjing");
            tm.put(new Student("lisi4", 24), "wuhan");
            tm.put(new Student("lisi4", 24), "wuhan"); //与上面相同不存
            for (Map.Entry<Student, String> entry : tm.entrySet()) {
                System.out.println(entry.getKey() + "___" + entry.getValue());
            }
    
            /**
             * -----------------第第一种取出方式 keySet, 迭代器-------------------------
             * lisi4,24___wuhan
             * lisi2,22___shanghai
             * lisi1,21___tianjin
             * lisi3,23___nanjing
             * -----------------第二种取出方式 entrySet, 迭代器-------------------------
             * lisi4,24___wuhan
             * lisi2,22___shanghai
             * lisi1,21___tianjin
             * lisi3,23___nanjing
             * ---------------第三种取出方式 for循环方式---------------------------
             * lisi4,24___wuhan
             * lisi2,22___shanghai
             * lisi1,21___tianjin
             * lisi3,23___nanjing
             * ----------------TreeMap自定义排序,使用比较器--------------------------
             * lisi1,21___tianjin
             * lisi2,22___shanghai
             * lisi3,23___nanjing
             * lisi4,24___wuhan
             */
        }
    }
    

      

  • 相关阅读:
    一周心得5:
    一周心得4:
    历史上两个人合作成功的案例:
    对结对编程的理解:
    一周心得3:
    一周心得2:
    有关IT行业模仿案例及自己的评价与解析:
    一周心得:
    不懂的问题:
    自我介绍以及期望与目标:
  • 原文地址:https://www.cnblogs.com/smartsmile/p/11616167.html
Copyright © 2011-2022 走看看