• Set集合接口-HashSet_TreeSet理解


    Set集合里面并不存在有get()方法。 Set本身也属于接口,而在Set接口下有两个常用的子类:HashSet、TreeSet。

    在以后的开发之中,Set集合一定不会作为我们的首选出现。使用最多的依然是List集合。

    1.无序存放:HashSet

     1 package cn.demo;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 public class TestHash {
     6     public static void main(String[] args) throws Exception {
     7         Set<String> set = new HashSet<String>();
     8         set.add("java");
     9         set.add("html"); 
    10         set.add("jsp"); 
    11         set.add("null"); 
    12         System.out.println(set);
    13     }
    14 }
    15     

    结果:

    [java, null, jsp, html]

    TreeSet有序存放,在TreeSet集合里面无法保存null数据类型。HashSet可以保存null.

    2.TreeSet排序说明

    既然TreeSet允许实现排序的处理操作,那么下面将采用自定义对象进行排序处理。那么此时对象所在的类一定要实现Comparable接口,否则在使用add()保存数据的时候就会出现“ClassCastException”。但是在使用此类操作时必须保证在compareTo()方法里面将所有的属性进行比较。

     1 package cn.demo;
     2 
     3 import java.util.Set;
     4 import java.util.TreeSet;
     5 class Mible implements Comparable<Mible>{
     6     private String brand;
     7     private double price; 
     8     public Mible(String brand,double price){
     9         this.brand = brand;
    10         this.price = price;
    11     }
    12     @Override 
    13     public String toString() {
    14         return "Mible [brand=" + brand + ", price=" + price + "
    ";
    15     }
    16 public int compareTo(Mible o){
    17     if(this.price> o.price){
    18         return 1;
    19     }else if (this.price< o.price){
    20         return -1;
    21     }else{
    22         return this.brand.compareTo(o.brand);
    23     }
    24  }    
    25 }
    26 public class Test {
    27     public static void main(String[] args) throws Exception {
    28         Set<Mible> set = new TreeSet<Mible>();
    29         set.add(new Mible("黑米",19.9));
    30         set.add(new Mible("黑米",19.9));
    31         set.add(new Mible("黑米",19.9));
    32         set.add(new Mible("黑米",4343.9));
    33         set.add(new Mible("小米",197.9));
    34         set.add(new Mible("白米",19.9));
    35         
    36         System.out.println(set);
    37     }    
    38 }

    结果:

    [Mible [brand=白米, price=19.9
    , Mible [brand=黑米, price=19.9
    , Mible [brand=小米, price=197.9
    , Mible [brand=黑米, price=4343.9
    ]

    结论:TreeSet子类判断重复元素的一句依靠的是compareTo()方法返回的是否为0。

    3.关于重复元素的说明

    Comparable只能够针对于排序的子类使用,而着并不是真正的所谓的重复元素的判断的依据,而在集合之中,对于重复元素的判断使用的是Object类中两个方法完成的:

    · 取得Hash码:public int hashCode();

    · 对象比较:public boolean equals(Object obj)。

    HashCode是一组根据对象中所有属性的内容自动计算出来的一个不会重复的数值。在进行数据判断的时候会首先通过HashCode找到存储的对象,但是只是依靠HashCode还不足以完全可靠,那么还需要进行身份的信息的匹配,那么此时依靠的是equals()方法。

       在Eclipse里面针对于HashCode()和equals()实际上并不需要用户自己去编写,可以直接通过工具自动生成:

    代码:

     1 package cn.demo;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 class Mible{
     6     private String brand;
     7     private double price; 
     8     public Mible(String brand,double price){
     9         this.brand = brand;
    10         this.price = price;
    11     }
    12     @Override 
    13     public String toString() {
    14         return "Mible [brand=" + brand + ", price=" + price + "
    ";
    15     }
    16     @Override
    17     public int hashCode() {
    18         final int prime = 31;
    19         int result = 1;
    20         result = prime * result + ((brand == null) ? 0 : brand.hashCode());
    21         long temp;
    22         temp = Double.doubleToLongBits(price);
    23         result = prime * result + (int) (temp ^ (temp >>> 32));
    24         return result;
    25     }
    26     @Override
    27     public boolean equals(Object obj) {
    28         if (this == obj)
    29             return true;
    30         if (obj == null)
    31             return false;
    32         if (getClass() != obj.getClass())
    33             return false;
    34         Mible other = (Mible) obj;
    35         if (brand == null) {
    36             if (other.brand != null)
    37                 return false;
    38         } else if (!brand.equals(other.brand))
    39             return false;
    40         if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
    41             return false;
    42         return true;
    43     }
    44 
    45 }
    46 public class Test {
    47     public static void main(String[] args) throws Exception {
    48         Set<Mible> set = new HashSet<Mible>();
    49         set.add(new Mible("黑米",19.9));
    50         set.add(new Mible("黑米",19.9));
    51         set.add(new Mible("黑米",4343.9));
    52         set.add(new Mible("小米",197.9));
    53         set.add(new Mible("白米",19.9));
    54         
    55         System.out.println(set);
    56     }    
    57 }

    结果:

    [Mible [brand=小米, price=197.9
    , Mible [brand=黑米, price=4343.9
    , Mible [brand=黑米, price=19.9
    , Mible [brand=白米, price=19.9
    ]

    总结:对于重复元素的判断依靠的是Object类中的hashCode()与equals()方法执行判断的。以后首选使用HashSet。

  • 相关阅读:
    POJ-1035 Spell checker---字符串处理
    hdu-3572 Task Schedule---最大流判断满流+dinic算法
    BZOJ4826: [Hnoi2017]影魔
    BZOJ4825: [Hnoi2017]单旋
    BZOJ3504: [Cqoi2014]危桥
    BZOJ4407: 于神之怒加强版
    BZOJ2818: Gcd
    BZOJ4542: [Hnoi2016]大数
    BZOJ4540: [Hnoi2016]序列
    BZOJ4537: [Hnoi2016]最小公倍数
  • 原文地址:https://www.cnblogs.com/liyang31/p/5811954.html
Copyright © 2020-2023  润新知