当前位置: 首页 > web >正文

第十二节:第五部分:集合框架:Set集合的特点、底层原理、哈希表、去重复原理

Set系列集合特点

Set系列集合特点

哈希值

哈希值

HashSet集合的底层原理

HashSet集合的底层原理1
HashSet集合的底层原理2
HashSet集合的底层原理3
HashSet集合的底层原理4

HashSet集合去重复

HashSet集合去重复

代码

代码一:整体了解一下Set系列集合的特点

package com.itheima.day20_Collection_set;import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;//目标:整体了解一下Set系列集合的特点。
public class SetTest1 {public static void main(String[] args) {// 1、创建一个Set集合的对象//Set<Integer> set = new HashSet<>();// 创建了一个HashSet的集合对象。一行经典代码 HashSet: 无序 不重复 无索引//Set<Integer> set = new LinkedHashSet<>();//有序  不重复  无索引Set<Integer> set = new TreeSet<>();//可排序(升序)    不重复 无索引set.add(666);set.add(666);set.add(666);set.add(888);set.add(888);set.add(888);set.add(777);set.add(777);System.out.println(set);}
}

结果1

代码二:了解一下哈希值。Java中的所有对象,都可以调用0bejct类提供的hashcode方法,返回该对象自己的哈希值

Student类(学生类)

package com.itheima.day20_Collection_set;import java.util.Objects;public class Student {private String name;private int age;private double height;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}
}

SetTest2 类(主程序)

package com.itheima.day20_Collection_set;
/*目标:了解一下哈希值。Java中的所有对象,都可以调用0bejct类提供的hashcode方法,返回该对象自己的哈希值
public int hashCode():返回对象的哈希值。
同一个对象多次调用hashcode()方法返回的哈希值是相同的。
不同的对象,它们的哈希值一般不相同,但也有可能会相同(哈希碰撞)。*/
public class SetTest2 {public static void main(String[] args) {Student s1 = new Student("飞鸟马时",17,165);Student s2 = new Student("枣伊吕波",17,155);System.out.println(s1.hashCode());System.out.println(s2.hashCode());String str1 = new String("abc");String str2 = new String("acD");System.out.println(str1.hashCode());System.out.println(str2.hashCode());}
}

结果2

代码三:自定义的类型的对象,比如两个内容一样的学生对象,如何让HashSet集合能够去重复

Student类(学生类)

package com.itheima.day20_Collection_set;import java.util.Objects;public class Student {private String name;private int age;private double height;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}//只要两个对象内容一样就返回true@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);}@Overridepublic int hashCode() {// 姓名 年龄 身高计算哈希值的return Objects.hash(name, age, height);}public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}
}

SetTest3 (主程序)

package com.itheima.day20_Collection_set;import java.util.HashSet;
import java.util.Set;//目标:自定义的类型的对象,比如两个内容一样的学生对象,如何让HashSet集合能够去重复!
public class SetTest3 {public static void main(String[] args) {Student s1 = new Student("飞鸟马时",17,165);Student s2 = new Student("枣伊吕波",17,155);Student s3 = new Student("枣伊吕波",17,155);Set<Student> students = new HashSet<>();students.add(s1);students.add(s2);students.add(s3);System.out.println(students.toString());}
}

去重复后

http://www.xdnf.cn/news/10687.html

相关文章:

  • 《QDebug 2025年5月》
  • 基于大模型的急性乳腺炎全病程风险预测与综合治疗方案研究
  • Playwright Python API 测试:从入门到实践
  • 滑动窗口 -- 灵神刷题
  • C# 异常处理进阶:精准获取错误行号的通用方案
  • ubuntu安装devkitPro
  • 什么算得到?什么又算失去?
  • ps曝光度调整
  • 继承(全)
  • 2024年数维杯国际大学生数学建模挑战赛D题城市弹性与可持续发展能力评价解题全过程论文及程序
  • YOLOv10改进|爆改模型|涨点|C2F引入空间和通道注意力模块暴力涨点(附代码+修改教程)
  • 九(4).存在指针的引用,不存在引用的指针
  • uniapp-商城-77-shop(8.2-商品列表,地址信息添加,级联选择器picker)
  • window ollama部署模型
  • 2025年主流编程语言全面分析与学习指南
  • 【MySQL】使用C语言连接数据库
  • Linux内核体系结构简析
  • 长尾关键词布局与SEO实战策略
  • PythonWeb项目开发脚手架
  • String和StringBuilder和StringBuffer
  • NodeJS全栈WEB3面试题——P3Web3.js / Ethers.js 使用
  • android binder(四)binder驱动详解
  • 【Block总结】LRSA,局部区域自注意力|即插即用
  • 有sudo权限下/无sudo权限下:切换gcc、g++版本
  • ipfs下载和安装(windows)
  • FastAPI+Pyomo实现线性回归解决饮食问题
  • 第十七章 数据集成
  • MySQL主从复制深度解析:原理、架构与实战部署指南
  • CodeTop100 Day20
  • 树欲静而风不止,子欲养而亲不待