研究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)
还没有任何评论哟~
