zoukankan      html  css  js  c++  java
  • Set容器——HashSet及常用API

    Set容器特点

    ①   Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序;

    ②   最常用的两个Set接口的实现类是HashSet和TreeSet;

     

    HashSet及常用API

    ①   HashSet扩展AbstractSet并且实现Set接口;

    ②   HashSet使用散列表(又称哈希表)进行存储;

    ③   构造方法:

    a)   HashSet()

    b)   HashSet(Collection c)

    c)   HashSet(int capacity)

    d)   HashSet(int capacity,float fillRatio)

    ④   HashSet没有定义任何超过它的父类和接口提供的其它方法;

    ⑤   散列集合没有确保其元素的顺序,因为散列处理通常不参与排序;

    1         HashSet<String> data=new HashSet<String>();
    2         data.add("张三");
    3         data.add("李四");
    4         data.add("jay");
    5         data.add("jack");
    6         data.add("jay");
    7         System.out.println(data);

    输出结果:

    [李四, 张三, jay, jack]

    此处第二个jay没有存入;

    可以将其打印出来System.out.println(data.add("jay"));,结果显示第一个为true,第二个为false

    编写一个Student类:

     1 class Student{
     2     private String name;
     3     private int age;
     4     public Student(String name, int age) {
     5         super();
     6         this.name = name;
     7         this.age = age;
     8     }
     9     public String getName() {
    10         return name;
    11     }
    12     public void setName(String name) {
    13         this.name = name;
    14     }
    15     public int getAge() {
    16         return age;
    17     }
    18     public void setAge(int age) {
    19         this.age = age;
    20     }
    21 }

    主方法中添加及输出

    1         HashSet<Student> stuSet=new HashSet<Student>();
    2         System.out.println(stuSet.add(new Student("张三",20)));
    3         System.out.println(stuSet.add(new Student("李四",30)));
    4         System.out.println(stuSet.add(new Student("张三",20)));
    5         System.out.println(stuSet.size());
    6     

    输出结果:

    true

    true

    true

    3

    由此可见:new Student("张三",20)两次都创建了,若想字相同时只创建一次则需重构hashCode和equals方法

    如下:

     1     @Override
     2     public int hashCode() {
     3         final int prime = 31;
     4         int result = 1;
     5         result = prime * result + age;
     6         result = prime * result + ((name == null) ? 0 : name.hashCode());
     7         return result;
     8     }
     9     @Override
    10     public boolean equals(Object obj) {
    11         if (this == obj)
    12             return true;
    13         if (obj == null)
    14             return false;
    15         if (getClass() != obj.getClass())
    16             return false;
    17         Student other = (Student) obj;
    18         if (age != other.age)
    19             return false;
    20         if (name == null) {
    21             if (other.name != null)
    22                 return false;
    23         } else if (!name.equals(other.name))
    24             return false;
    25         return true;
    26     }

    再次执行,输出结果:

    true

    true

    false

    2

    总结:HashSet的内部操作的底层数据是HashMap,只是我们操作的是HashMap的key;

  • 相关阅读:
    IDEA-各模块间引用出现问题的解决方法
    【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String
    《转载》JVM垃圾回收机制
    java面试复习题四
    Java中excel转换为jpg/png图片 采用aspose-cells-18.6.jar
    POI导出复杂的excel;excel公共样式类;excel拼接定制类;数据科学计数法转为普通值
    java发送邮件无法显示图片 图裂 的解决办法
    pom.xml文件最详细的讲解
    Tomcat启动报Error listenerStart错误 Context [] startup failed due to previous errors
    ora-01031:insufficient privileges解决方法
  • 原文地址:https://www.cnblogs.com/wzy330782/p/5402603.html
Copyright © 2011-2022 走看看