当前位置:网站首页>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());
}
}
}

边栏推荐
- Clause 33: decltype is used for auto & type formal parameters, with std:: forward
- Luogu p1088 Martians
- C language learning log 10.5
- Robot pose description and coordinate transformation
- Ruoyi cloud startup tutorial (hand-held graphics)
- Std:: Map initialization
- Shell built-in string substitution
- Chapter 15 mechanism: Address Translation
- Must know must know -c language keywords
- Leetcode game 297 (20220612)
猜你喜欢

Leetcode game 297 (20220612)

What is the saturate operation in opencv

What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM

QT direction key to move focus

Analysis on the similarities and differences of MVC, MVP and mvvc

Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases

Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis

Interpretation of QT keypressevent

Section 6 - pointers

详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
随机推荐
QT interface rendering style
Chapter 18 pagination: Introduction
Bm1z002fj-evk-001 startup evaluation
Brick story
Advantages of win8.1 and win10
Force buckle 25 A group of K flipped linked lists
How to lay copper in AD (aluminum designer)
135. distribute candy
2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
Kaggle time series tutorial
LeetCode第297场周赛(20220612)
C language learning log 10.8
C language learning log 10.6
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
【转载】C语言内存和字符操作函数大全
Std:: Map initialization
Bomb disposal cat
Explain the role of key attribute in V-for
Robot pose description and coordinate transformation
Clause 28: understanding reference folding