• HashSet和TreeSet


    package com.wzy.list;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;
    
    
    class Student1 implements Comparable{
        private Integer id;
        private String name;
        
        public Student1(String name,Integer id) {
            this.id = id;
            this.name = name;
        }    
        @Override
        public String toString() {
            return "name:"+name+"; id:"+id;
        }
        @Override
        public int compareTo(Object o) {
            if(this.id > ((Student1)o).id) {
                return 1;
            }else if(this.id < ((Student1)o).id) {
                return -1;
            }else{
                return this.name.compareTo(((Student1)o).name);
            }
            
        }
    }
    
    
    class Student2{
        private Integer id;
        private String name;
        
        public Student2(String name,Integer id) {
            this.id = id;
            this.name = name;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((id == null) ? 0 : id.hashCode());
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Student2 other = (Student2) obj;
            if (id == null) {
                if (other.id != null)
                    return false;
            } else if (!id.equals(other.id))
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
    
        @Override
        public String toString() {
            return "Student2 [id=" + id + ", name=" + name + "]";
        }    
        
        
    }
    public class Test02 {
        public static void main(String[] args) {
            /**
             * TreeSet和HashSet存入对象时,不会自动去重操作
             * 
             * TreeSet去掉重复对象,基于Comparable接口实现
             * HashSet去掉重复对象,基于equals和hashCode实现
             * TreeSet可以对象排序,hashSet不可以
             * 
             * 如果存入的不是new出来的对象,
             * 而是String或int类型,可以直接使用,不必实现上述操作
             * **/
            
            //TreeSet存入对象实例
            Set<Student1> stu1 = new TreeSet<Student1>();
            stu1.add(new Student1("aa",1001));
            stu1.add(new Student1("bb",1001));//id重复
            stu1.add(new Student1("dd",1004));
            stu1.add(new Student1("cc",1003));
            stu1.add(new Student1("aa",1001));//对象重复
            Iterator s = stu1.iterator();
            while(s.hasNext()) {
                System.out.println(s.next());
            }
            
            //HashSet存入对象实例
            Set<Student2> stu2 = new HashSet<Student2>();
            stu2.add(new Student2("aa",1001));
            stu2.add(new Student2("bb",1003));
            stu2.add(new Student2("cc",1005));
            stu2.add(new Student2("dd",1004));
            stu2.add(new Student2("ee",1002));
            stu2.add(new Student2("aa",1001));
            
            Iterator i = stu2.iterator();
            while(i.hasNext()) {
                System.out.println(i.next());
            }
            
            //TreeSet存入普通字符串
            Set set1 = new TreeSet();
            set1.add("a");
            set1.add("aa");
            set1.add("d");
            set1.add("b");
            set1.add("c");
            //[a, aa, b, c, d] TreeSet已经自动排序好了
            System.out.println(set1);
            
            //HashSet存入普通字符串
            Set set2 = new HashSet();
            set2.add("a");
            set2.add("aa");
            set2.add("d");
            set2.add("c");
            set2.add("b");
            //[aa, a, b, c, d] HashSet不会排序
            System.out.println(set2);
        }
    }
  • 相关阅读:
    一致性哈希的理解与实践
    nil in Go
    为什么Go没有math.Min/Max(int, int) 函数?
    What happens when I type kubectl run?
    kubelet简要分析
    编译安装nginx和模块
    nginx与tengine添加check模块(nginx_upstream_check_module)
    多台ESXI 6.5 添加 iSCSI 共享存储 --centos 7.4 作为target
    Cannot open the disk '/vmfs/volumes/5e97f429-a56d6ea0-1ef3-000c29a09445/oracle_node1/oracle_node1_1.vmdk' or one of the snapshot disks it depends on.
    RabbitMQ windows2016 镜像模式 haproxy+keepalived
  • 原文地址:https://www.cnblogs.com/wwzyy/p/5542849.html
Copyright © 2020-2023  润新知