当前位置:网站首页>集合 set接口
集合 set接口
2022-08-10 05:32:00 【hagong9】
目录
set接口基本介绍
package com.collection_.set;
import java.util.HashSet;
public class HashSet_ {
public static void main(String[] args) {
HashSet set = new HashSet();
//执行add方法后会返回一个boolean值,添加成功为true 失败为false
// 可以通过remove指定删除哪个对象,
System.out.println(set.add("john"));//T
System.out.println(set.add("lucy"));//T
System.out.println(set.add("jack"));//T
System.out.println(set.add("Rose"));//T
System.out.println(set.add("john"));//F
System.out.println(set);
System.out.println("========移除john");
set.remove("john");
System.out.println(set);
//重置set
set = new HashSet();
System.out.println(set);//set为空
//HashSet 不能添加相同元素?
set.add("lucy");//T
set.add("lucy");//F
//这里两个jack都加入的原因是它们是new出来的,本质上是两个不同的数据,只是名字恰巧都叫jack而已。
set.add(new Dog("jack"));//T
set.add(new Dog("jack"));//T
set.add(new String("rose"));//T
set.add(new String("rose"));//F
System.out.println(set);
}
}
class Dog{
private String name;
public Dog(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
数组链表模拟
例子
package com.collection_.set;
import java.util.HashSet;
//建立是一个数组table 大小为16,在索引为2的地方存放一个链表
public class HashSetStructure {
public static void main(String[] args) {
Node[] table = new Node[16];
System.out.println(table);
//在索引为2的地方创建结点john
Node john = new Node("john", null);
table[2] = john;
//创建结点jack
Node jack = new Node("jack", null);
john.next = jack;//将jack挂载到john
System.out.println(table);
//创建结点rose
Node rose = new Node("rose", null);
jack.next = rose;//将rose挂载到jack后
}
}
class Node{
Object item;
Node next;
public Node(Object item, Node next) {
this.item = item;
this.next = next;
}
}
结构,如果要才索引3也创建结点,步骤也一样
HashSet底层代码
第6点补充:元素个数到达8个就行,而不是超过8。如果元素个数到达8而table大小<64,会对table进行扩容。
小练习
package com.collection_.set;
//添加3个人,如果名字和年龄相同则判定为同一个人,就只添加一个。
import java.util.HashSet;
import java.util.Objects;
public class HashSetTest01 {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add(new People("张三",18));
set.add(new People("李四",28));
set.add(new People("李四",28));
set.add(new People("王五",38));
System.out.println(set);
}
}
class People{
private String name;
private int age;
@Override
public String toString() {
return "people{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public People(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
//ale+ins 重写equals和hashSet
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People people = (People) o;
return age == people.age &&
Objects.equals(name, people.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
练习2
package com.collection_.set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
@SuppressWarnings("all")
public class HashSetTest02 {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add(new People2("张三",8000,new Mydate(2000,2,12)));
set.add(new People2("张三",9000,new Mydate(2000,2,12)));
set.add(new People2("李四",9000,new Mydate(2000,6,1)));
set.add(new People2("王五",10000,new Mydate(1998,6,29)));
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}
}
class People2{
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People2 people2 = (People2) o;
return Objects.equals(name, people2.name) &&
Objects.equals(brithday, people2.brithday);
}
@Override
public int hashCode() {
return Objects.hash(name, brithday);
}
private String name;
private int sal;
private Mydate brithday;
@Override
public String toString() {
return "People2{" +
"name='" + name + '\'' +
", sal=" + sal +
", brithday=" + brithday +
'}';
}
public People2(String name, int sal, Mydate brithday) {
this.name = name;
this.sal = sal;
this.brithday = brithday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public Mydate getBrithday() {
return brithday;
}
public void setBrithday(Mydate brithday) {
this.brithday = brithday;
}
}
class Mydate{
private int year;
private int month;
private int day;
@Override
public String toString() {
return "brithday="+ year + "/" +month +"/" + day;
}
public Mydate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mydate mydate = (Mydate) o;
return year == mydate.year &&
month == mydate.month &&
day == mydate.day;
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
}
LinkedHashSet
和HashSet差不多就是会按照添加顺序打印。
treeset
package com.collection_.set;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSet_ {
public static void main(String[] args) {
TreeSet treeSet = new TreeSet();
treeSet.add("50");
treeSet.add("38");
treeSet.add("john");
treeSet.add("som");
treeSet.add("an");
treeSet.add("tom");
treeSet.add("ber");
//当使用无参构造器,创建treeset时,仍然是无序的
System.out.println(treeSet);
//使用TreeSet提供的一个构造器,可以提供一个比较器(匿名内部类)
//并指定排序规则
TreeSet treeSet1 = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
//调用String的compareto方法
return ((String)o1).compareTo((String)o2);//按照ASCII码顺序排序 ASCII码长度相等后面的就添加不进去
//return ((String)o1).length() - ((String)o2).length();//按照长度排序,就是如果长度相同(相减等于0)就不能添加进去
}
});
treeSet1.add("50");
treeSet1.add("38");
treeSet1.add("john");
treeSet1.add("som");
treeSet1.add("an");
treeSet1.add("tom");
treeSet1.add("ber");
System.out.println(treeSet1);
}
}
底层代码
边栏推荐
猜你喜欢
I use this recruit let the team to improve the development efficiency of 100%!
view【】【】【】【】
Batch add watermark to pictures batch add background zoom batch merge tool picUnionV4.0
索引笔记【】【】
图片批量添加水印批量加背景缩放批量合并工具picUnionV4.0
GtkD开发之路
IDEA 项目中设置 Sources Resources 等文件夹
最新最全的数字藏品发售日历-07.26
ORACLE系统表空间SYSTEM占满无法扩充表空间问题解决过程
ORACLE system table space SYSTEM is full and cannot expand table space problem solving process
随机推荐
【el和template区别】
速刷正则表达式一周目(上)
Operation table Function usage
One step ahead, don't miss it again, the chain reading APP will be launched soon!
tinymce富文本编辑器
训练集Loss收敛,但是测试集Loss震荡的厉害?
网安超基础一周目
Pony语言学习(一):环境配置(续)
YOLOv5 PyQt5(一起制作YOLOv5的GUI界面)
R语言:修改chart.Correlation()函数绘制相关性图——完美出图
pytorch框架学习(7) tensorboard使用
链读|最新最全的数字藏品发售日历-07.29
国内数字藏品投资价值分析
R绘制图像,图像特征提取
opencv
25张炫酷交互图表,一文入门Plotly
Canal 报错 Could not find first log file name in binary log index file
Pony语言学习(六):Struct, Type Alias, Type Expressions
shell脚本中利用sqlplus操作数据库
【写下自用】每次都忘记如何train?记录如何训练自己的yolov5