当前位置:网站首页>Case -- the HashSet set stores the student object and traverses
Case -- the HashSet set stores the student object and traverses
2022-06-13 05:05:00 【Jason_ LH1024】


package com.it;
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;
}
// rewrite equals and hashCode Method
@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);
}
}
package com.it;
import java.util.HashSet;
public class HashSetDemo {
public static void main(String[] args) {
// establish HashSet A collection of objects
HashSet<Student> hs = new HashSet<>();
Student s1 = new Student(" Huairong ", 23);
Student s2 = new Student(" brother ", 22);
Student s3 = new Student(" Sure ", 21);
Student s4 = new Student(" Huairong ", 23);
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
for (Student s : hs) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}

边栏推荐
- C language learning log 12.25
- Sort (internal sort) + external sort
- Kaggle time series tutorial
- [reprint] complete collection of C language memory and character operation functions
- Several methods of identifying equivalent circuit of circuit drawing
- C language learning log 1.19
- Gradient descent, learning rate
- List collection concurrent modification exception
- C language learning log 10.8
- 【多线程编程】Future接口获取线程执行结果数据
猜你喜欢

Explain the role of key attribute in V-for

RuoYi-Cloud启动教程(手把手图文)

Logical point

QT brushes and brushes

C language learning log 12.5

2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation

Advanced C - Section 2 - pointers

Mind mapping series - Database

Section 5 - Operator details

CMB's written test -- data analysis
随机推荐
C language learning log 1.22
Redis plus instructions
JS to realize the conversion between string and array and an interview question
std::condition_ variable::wait_ for
Luogu p1036 number selection
Bm1z002fj-evk-001 startup evaluation
Section 7 - structures
Reductive elimination
Analysis on the similarities and differences of MVC, MVP and mvvc
Chapter 15 mechanism: Address Translation
Understanding inode
Draw a hammer
COAP protocol libcoap API
Shell built-in string substitution
Clause 32: moving objects into closures using initialization capture objects
On switch() case statement in C language
C language learning log 11.7
List collection concurrent modification exception
josephus problem
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions