Advertisement

研究HashSet容器存储自定义对象的机制

阅读量:

首先设计并构建一个用于表示学生的类结构

复制代码
>       1. class Student{

>  
>       2.
>  
>       3.      String id;
>  
>       4.  
>  
>       5.      public Student(String id) {
>  
>       6.              this.id = id;
>  
>       7.      }
>  
>       8. }
>  
>  
>  
>  
> ```

接下来将学生类型的数据对象存入HashSet集合中:
复制代码
  1. HashSet<Student>set = new HashSet<>();
复制代码
  2. set.add(new Student("1"));

  3. set.add(new Student("1"));
复制代码

对基础层代码进行解析:

复制代码
>       1. public boolean add(E e) {

>  
>       2.         return map.put(e, PRESENT)==null;
>  
>       3.     }
>  
>  
> 

全部评论 (0)

还没有任何评论哟~