zoukankan      html  css  js  c++  java
  • 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());
            }
            
        
        }
    }
  • 相关阅读:
    12306站点推出图片验证 反破解
    android自己定义控件之飞入飞出控件
    ORACLE 从一个实例迁移到另外一个实例实战记录
    通信协议:HTTP、TCP、UDP
    先打11.2.0.3.8这个PSU,后建库
    C# 多线程參数传递
    运维笔记10 (Linux软件的安装与管理(rpm,yum))
    为RecyclerView打造通用Adapter
    大话设计模式(四)单例模式
    Java代码质量监控工具Sonar安装
  • 原文地址:https://www.cnblogs.com/xiarongjin/p/8309709.html
Copyright © 2011-2022 走看看