当前位置:   article > 正文

Java基础案例教程 第六章集合类 ———6.4 Set接口_set set =new hashset set.add("jack

set set =new hashset set.add("jack

一、Set接口简介

  • Set接口和List接口一样,同样继承自Collection接口
  • 它与Collection接口中的方法基本一致,没有对Collection接口进行功能上的扩充,只是比Collection接口更加严格了
  • 与list接口不同的是,Set接口中的元素无序,并且都会以某种规则保证存入的元素不出现重复
  • Set接口主要有两个实现类,分别是HashSet 和 TreeSet。

           HashSet是根据对象的哈希值来确定元素在集合里的存储位置,因此具有良好的存取和查找性能

           TreeSet则是以二叉树的方式来存取

 

1、HashSet

(1)存入相同String

  1. package cn.itcast.chapter06.example07;
  2. import java.util.*;
  3. /**
  4. * HashSet集合的用法
  5. */
  6. public class Example07 {
  7. public static void main(String[] args) {
  8. HashSet set = new HashSet(); // 创建HashSet集合
  9. set.add("Jack"); // 向该Set集合中添加字符串
  10. set.add("Eve");
  11. set.add("Rose");
  12. set.add("Rose"); // 向该Set集合中添加重复元素
  13. Iterator it = set.iterator(); // 获取Iterator对象
  14. while (it.hasNext()) { // 通过while循环,判断集合中是否有元素
  15. Object obj = it.next(); // 如果有元素,就通过迭代器的next()方法获取元素
  16. System.out.println(obj);
  17. }
  18. }
  19. }
  20. 输出
  21. Eve
  22. Rose
  23. Jack

 

(2)存入“ 相同 ” 对象

        只要new 出一个对象,内存里就会分配一片内存空间,即使成员变量相同,也是不同的对象

  1. package cn.itcast.chapter06.example08;
  2. import java.util.HashSet;
  3. class Student {
  4. String id;
  5. String name;
  6. public Student(String id, String name) { // 创建构造方法
  7. this.id = id;
  8. this.name = name;
  9. }
  10. public String toString() { // 重写toString()方法
  11. return id + ":" + name;
  12. }
  13. }
  14. /**
  15. * HashSet集合的用法
  16. */
  17. public class Example08 {
  18. public static void main(String[] args) {
  19. HashSet hs = new HashSet(); // 创建HashSet集合
  20. Student stu1 = new Student("1", "Jack"); // 创建Student对象
  21. Student stu2 = new Student("2", "Rose");
  22. Student stu3 = new Student("2", "Rose"); //内存空间不同
  23. hs.add(stu1);
  24. hs.add(stu2);
  25. hs.add(stu3);
  26. System.out.println(hs);
  27. }
  28. }
  29. 输出
  30. [2:Rose, 1:Jack, 2:Rose]

 

(3) void add(Object obj)详解

         在调用add() 时,会首先调用hashCode()方法,获得当前待存入对象的哈希值,然后根据哈希值算出一个存储位置。

                 如果该位置上没有元素,直接存入;

                 有元素,则调用equals()方法比较两个元素:

                         返回false则存入,

                         返回true则舍弃

  1. //3个对象的哈希值不同
  2. package cn.itcast.chapter06.example09;
  3. import java.util.*;
  4. class Student {
  5. private String id;
  6. private String name;
  7. public Student(String id, String name) {
  8. this.id = id;
  9. this.name = name;
  10. }
  11. // 重写toString()方法
  12. public String toString() {
  13. return id + ":" + name;
  14. }
  15. }
  16. public class Example09 {
  17. public static void main(String[] args) {
  18. HashSet hs = new HashSet(); // 创建HashSet对象
  19. Student stu1 = new Student("1", "Jack"); // 创建Student对象
  20. Student stu2 = new Student("2", "Rose");
  21. Student stu3 = new Student("2", "Rose");
  22. System.out.println(stu1.hashCode());
  23. System.out.println(stu2.hashCode());
  24. System.out.println(stu3.hashCode());
  25. hs.add(stu1); // 向集合存入对象
  26. hs.add(stu2);
  27. hs.add(stu3);
  28. System.out.println(hs); // 打印集合中的元素
  29. }
  30. }
  31. 输出
  32. 1163157884
  33. 1956725890
  34. 356573597
  35. [2:Rose, 1:Jack, 2:Rose]
  1. // 重写hashCode(), equals方法
  2. package cn.itcast.chapter06.example09;
  3. import java.util.HashSet;
  4. class Student {
  5. private String id;
  6. private String name;
  7. public Student(String id, String name) {
  8. this.id = id;
  9. this.name = name;
  10. }
  11. // 重写toString()方法
  12. public String toString() {
  13. return id + ":" + name;
  14. }
  15. // 重写hashCode方法
  16. public int hashCode() {
  17. return id.hashCode(); // 返回id属性的哈希值
  18. }
  19. // 重写equals方法
  20. public boolean equals(Object obj) {
  21. if (this == obj) { // 判断是否是同一个对象
  22. return true; // 如果是,直接返回true
  23. }
  24. if (!(obj instanceof Student)) { // 判断对象是为Student类型
  25. return false; // 如果对象不是Student类型,返回false
  26. }
  27. Student stu = (Student) obj; // 将对象强转为Student类型
  28. boolean b = this.id.equals(stu.id); // 判断id值是否相同
  29. return b; // 返回判断结果
  30. }
  31. }
  32. public class Example09 {
  33. public static void main(String[] args) {
  34. HashSet hs = new HashSet(); // 创建HashSet对象
  35. Student stu1 = new Student("1", "Jack"); // 创建Student对象
  36. Student stu2 = new Student("2", "Rose");
  37. Student stu3 = new Student("2", "Rose");
  38. System.out.println(stu1.hashCode());
  39. System.out.println(stu2.hashCode());
  40. System.out.println(stu3.hashCode());
  41. hs.add(stu1); // 向集合存入对象
  42. hs.add(stu2);
  43. hs.add(stu3);
  44. System.out.println(hs); // 打印集合中的元素
  45. }
  46. }
  47. 输出
  48. 49
  49. 50
  50. 50
  51. [1:Jack, 2:Rose]

 

二、

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/763581
推荐阅读
  

闽ICP备14008679号