当前位置:网站首页>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());
}
}
}
边栏推荐
- Hidden implementation and decoupling, knowing Pimpl mode
- OpenCV中的saturate操作(饱和操作)究竟是怎么回事
- Dup2 use
- C language learning log 11.7
- Explain the role of key attribute in V-for
- Force buckle 92 Reverse linked list II
- List collection concurrent modification exception
- Recursion and recursion
- 【转载】C语言内存和字符操作函数大全
- Section 3 - functions
猜你喜欢
Chapter 15 mechanism: Address Translation
Logical point
Mysql8.0.13 installation tutorial (with pictures)
Section 4 - arrays
Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display
Dynamic and static libraries
Win8.1和Win10各自的優勢
Simple greedy strategy
Bomb disposal cat
About mission planning and improving execution
随机推荐
Leetcode game 297 (20220612)
How to understand JS expressions and JS statements
Clause 30: be familiar with the failure of perfect forwarding
Clause 48: understand template metaprogramming
【转载】C语言内存和字符操作函数大全
RTSP streaming using easydarwin+ffmpeg
C language learning log 2.19
语音信号分帧的理解
C language learning log 2.6
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
C language learning log 12.14
QT signal is automatically associated with the slot
QT direction key to move focus
1.2 differences caused by keywords
Dup2 use
Clause 32: moving objects into closures using initialization capture objects
Luogu p1036 number selection
Wang Dao Chapter II linear table exercises
Binary search and binary answer
C language exercise 1