Skip to content

Instantly share code, notes, and snippets.

@zbstof
Created April 17, 2019 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbstof/d58d9236ee0de278b1403d0bb992b2d0 to your computer and use it in GitHub Desktop.
Save zbstof/d58d9236ee0de278b1403d0bb992b2d0 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
Set<Student> set = new HashSet<Student>();
Student s = new Student("Маша", "Иванова");
set.add(s);
s.setName("Даша");
set.add(s);
System.out.println(set.size());
}
class Student{
private String name;
private String surname;
Student(String name, String surname) {
this.name = name;
this.surname = surname;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (!name.equals(student.name)) return false;
if (!surname.equals(student.surname)) return false;
return true;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + surname.hashCode();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment