首页 > 其他分享 >HashSet的使用以及存储方式

HashSet的使用以及存储方式

时间:2023-02-20 12:34:18浏览次数:35  
标签:存储 name 方式 HashSet age System Student println out

package edu.wtbu;
import java.util.HashSet;
import java.util.Iterator;

public class Demo01 {
public static void main(String[] args) {
//HashSet: 存储结构:哈希表(数组+链表+红黑树)
//存储过程:1.根据hashCode计算保存的位置,如果此位置为空,则直接保存;如果不为空,则执行equals方法,如果equals方法为true,则认为是重复,否则形成链表

//创建集合
HashSet<String> hashSet = new HashSet<>();

//1.添加元素
hashSet.add("张三");
hashSet.add("李四");
hashSet.add("王五");
hashSet.add("老六");
//hashSet.add("老六"); 重复,不能添加
System.out.println("元素个数:"+hashSet.size());//元素个数:4
System.out.println(hashSet);//[李四, 张三, 老六, 王五]

//2.删除元素
hashSet.remove("老六");
System.out.println("元素个数:"+hashSet.size());//元素个数:3
System.out.println(hashSet);//[李四, 张三, 王五]

//3.遍历
//1.增强for
for (String s:
hashSet) {
System.out.println(s);
}

//2.使用迭代器
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}

//4.判断
System.out.println(hashSet.contains("张三"));//true
System.out.println(hashSet.isEmpty());//false

System.out.println("==============================");
//创建集合
HashSet<Student> hashSet1 = new HashSet<>();

//1.添加元素
Student s1 = new Student("张三", 16);
Student s2 = new Student("李四", 17);
Student s3 = new Student("王五", 18);
hashSet1.add(s1);
hashSet1.add(s2);
hashSet1.add(s3);
hashSet1.add(new Student("王五", 18));
//hashSet1.add(s3); 重复,不能添加
System.out.println("元素个数:"+hashSet1.size());//元素个数:3
System.out.println(hashSet1);//[Student{name='张三', age=16}, Student{name='李四', age=17}, Student{name='王五', age=18}]

//2.删除元素
//hashSet1.remove(s1);
hashSet1.remove(new Student("张三", 16));
System.out.println("元素个数:"+hashSet1.size());//元素个数:2
System.out.println(hashSet1);//[Student{name='李四', age=17}, Student{name='王五', age=18}]

//3.遍历
//1.增强for
for (Student s:
hashSet1) {
System.out.println(s);
}

//2.使用迭代器
Iterator<Student> iterator1 = hashSet1.iterator();
while (iterator1.hasNext()){
System.out.println(iterator1.next());
}

//4.判断
System.out.println(hashSet1.contains(s2));//true
System.out.println(hashSet1.isEmpty());//false
}
}


package edu.wtbu;

import java.util.Objects;

public class Student {
private String name;
private int age;

public Student() {

}

public Student(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;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

// @Override
// public int hashCode() {
// int n1 = this.name.hashCode();
// int n2 = this.age;
// return n1+n2;
// }
//
// @Override
// public boolean equals(Object obj) {
// if(obj==this){
// return false;
// }
// if(obj==null){
// return false;
// }
// if(obj instanceof Student){
// Student s = (Student) obj;
// if(this.name.equals(s.getName())&&this.age==s.getAge()){
// return true;
// }
// }
// return false;
// }

//快捷键:Alt+insert+equals and (hashCode)
@Override
public 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 && Objects.equals(name, student.name);
}

@Override
public int hashCode() {
return Objects.hash(name, age);
}
}

标签:存储,name,方式,HashSet,age,System,Student,println,out
From: https://www.cnblogs.com/123456dh/p/17136908.html

相关文章