当前位置:网站首页>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());
}
}
}
边栏推荐
- Red Treasure Book Reading Notes (continuously updated)
- C language learning log 1.17
- Avantages de win8.1 et win10
- QT signal is automatically associated with the slot
- C language learning log 1.22
- Robot pose description and coordinate transformation
- System file interface open
- Common skills in embedded programming
- C language exercise 1
- Clause 27: alternatives to overloading with familiar universal reference types
猜你喜欢
Section 5 - Operator details
Advanced C - Section 2 - pointers
Win8.1和Win10各自的優勢
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
Reductive elimination
C language exercise 1
CMB's written test -- data analysis
C language learning log 11.7
How to lay copper in AD (aluminum designer)
File descriptorfile description
随机推荐
Clause 34: lambda is preferred over std:: bind
Sampo Lock
What is the saturate operation in opencv
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
C language learning log 1.2
Understanding inode
QT using layout manager is invalid or abnormal
C language exercise 1
CMB written test graphical reasoning
Ruoyi cloud startup tutorial (hand-held graphics)
Clause 30: be familiar with the failure of perfect forwarding
Must know must know -c language keywords
OJ problem solution summary
使用EasyDarwin+FFmpeg实现rtsp推流
Embedded hardware - read schematic
Logical point
Robot pose description and coordinate transformation
Recursion and recursion
QT brushes and brushes
Nonstandard compiler attribute extension