• Java Set


    Set:元素不可以重复,是无序。
    Set接口中的方法和Collection一致。
    |--HashSet: 内部数据结构是哈希表 ,是不同步的。
    如何保证该集合的元素唯一性呢?
    是通过对象的hashCode和equals方法来完成对象唯一性的。
    如果对象的hashCode值不同,那么不用判断equals方法,就直接存储到哈希表中。 
    如果对象的hashCode值相同,那么要再次判断对象的equals方法是否为true。
    如果为true,视为相同元素,不存。如果为false,那么视为不同元素,就进行存储。
     
    记住:如果元素要存储到HashSet集合中,必须覆盖hashCode方法和equals方法。
    一般情况下,如果定义的类会产生很多对象,比如人,学生,书,通常都需要覆盖equals,hashCode方法。
    建立对象判断是否相同的依据。
     
     
     
     
    |--TreeSet:可以对Set集合中的元素进行排序。是不同步的。 
    判断元素唯一性的方式:就是根据比较方法的返回结果是否是0,是0,就是相同元素,不存。 
     
    TreeSet对元素进行排序的方式一:
    让元素自身具备比较功能,元就需要实现Comparable接口。覆盖compareTo方法。
     
    如果不要按照对象中具备的自然顺序进行排序。如果对象中不具备自然顺序。怎么办?
    可以使用TreeSet集合第二种排序方式二:
    让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。
    将该类对象作为参数传递给TreeSet集合的构造函数。
     
     
    if(this.hashCode()== obj.hashCode() && this.equals(obj))
     
    哈希表确定元素是否相同
    1,判断的是两个元素的哈希值是否相同。
    如果相同,在判断两个对象的内容是否相同。
     
    2,判断哈希值相同,其实判断的是对象的hashCode的方法。判断内容相同,用的是equals方法。
     
    注意:如果哈希值不同,是不需要判断equals。
     

    demo1:

    package com.cwcec.test;
    import java.util.HashSet;
    import java.util.Iterator;
    
    class Apple
    {
        private String color;
        private int weight;
        
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public double getWeight() {
            return weight;
        }
    
        public void setWeight(int weight) {
            this.weight = weight;
        }
    
        public Apple(String color,int weight)
        {
            this.color = color;
            this.weight = weight;
        }
    
        @Override
        public int hashCode() {
            System.out.println("hashcode:.....");
            return color.hashCode() + weight * 27;
        }
    
        @Override
        public boolean equals(Object obj) {
            
            System.out.println("equals:.....");
            if(this == obj)
                return true;
            if(!(obj instanceof Apple))
            {
                throw new ClassCastException("类型错误!");
            }
            
            Apple e = (Apple)obj;
            return color == e.color && weight == e.weight;
                
        }    
    }
    
    
    public class Person {
    
        public static void main(String[] args) {
    
            HashSet hs = new HashSet();
            hs.add(new Apple("red", 25));
            hs.add(new Apple("black", 75));
            hs.add(new Apple("black", 75));
            
            Iterator it = hs.iterator();
            while(it.hasNext())
            {
                Apple apple = (Apple)it.next();
                System.out.println(apple.getColor() + "," + apple.getWeight());
            }
            System.out.println("--------------------------");
            System.out.println(hs.contains(new Apple("black", 75)));
            System.out.println("--------------------------");
            System.out.println(new Apple("black", 75).equals(new Apple("black", 75)));
        
        }
    }
    Java Set - darrell - DARRELL的博客
     
     

    HashSet 中判断元素是否存在用到了hashCode和equals 方法;

    而ArrayList中判断元素是否存在只用到了equals 方法;

    demo2:

    package com.cwcec.test;
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.TreeSet;
    
    class Apple implements Comparable
    {
        private String color;
        private int weight;
        
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public int getWeight() {
            return weight;
        }
    
        public void setWeight(int weight) {
            this.weight = weight;
        }
    
        public Apple(String color,int weight)
        {
            this.color = color;
            this.weight = weight;
        }
    
        @Override
        public int hashCode() {
            System.out.println("hashcode:.....");
            return color.hashCode() + weight * 27;
        }
    
        @Override
        public boolean equals(Object obj) {
            
            System.out.println("equals:.....");
            if(this == obj)
                return true;
            if(!(obj instanceof Apple))
            {
                throw new ClassCastException("类型错误!");
            }
            
            Apple e = (Apple)obj;
            return color == e.color && weight == e.weight;
                
        }
    
        @Override
        public int compareTo(Object o) {          //让元素自身具备比较功能
            if(!(o instanceof Apple))
            {
                throw new ClassCastException("类型错误!");
            }
            Apple e = (Apple)o;
            int temp = this.getColor().compareTo(e.getColor());
            
            return temp == 0 ? this.getWeight() - e.getWeight() : temp;
    //        return 1;
        }    
        
    }
    
    class CompareByColor implements Comparator    //让集合自身具备比较功能,比较器
    {
    
        @Override
        public int compare(Object o1, Object o2) {
            Apple a1 = (Apple)o1;
            Apple a2 = (Apple)o2;
            int temp = a1.getColor().compareTo(a2.getColor());
            return temp == 0 ? a1.getWeight() - a2.getWeight() : temp;
        }
    }
    
    
    public class Person {
    
        public static void main(String[] args) {
    
            TreeSet hs = new TreeSet(new CompareByColor());
            
            hs.add(new Apple("red", 25));
            hs.add(new Apple("black", 55));
            hs.add(new Apple("red", 25));
            hs.add(new Apple("white", 75));
            
            Iterator it = hs.iterator();
            while(it.hasNext())
            {    
                Apple a = (Apple)it.next();
                System.out.println(a.getColor() + ":" + a.getWeight());
            }
            
        
        }
    }
  • 相关阅读:
    爱斯达M2C服装定制系统介绍—在线播放—优酷网,视频高清在线观看
    衣云网,依托深圳发达的服装产业,致力于整合服装上下游各个服装利益者,以自主研发的服装软件为核心,聚集服装设计师、纸样师和版师,这三类会员在衣云网开设工作室上传原创服装纸样,推荐相应纸样的面料吸引大批的面辅料商和服装企业。从而形成一个良性的服装生态圈。
    男士休闲装设计
    新郎胸花佩戴法则 小胸花也有大学问_新郎_婚礼时光-关注婚礼的一切,分享最美好的时光。
    上海游侠电动汽车团队招募。iOS,Android,产品经理以及 SEVER 端工程师
    沈晨:衣冠自成气场·都市周报
    寻访上海西服定制店_Enjoy·雅趣频道_财新网
    金错刀对话口袋购物王珂:找到痛点,确认卖点,制造爆点!
    Wish | IT桔子
    Tradesy | IT桔子
  • 原文地址:https://www.cnblogs.com/xiarongjin/p/8309709.html
Copyright © 2020-2023  润新知