Created
April 17, 2019 13:27
-
-
Save zbstof/d58d9236ee0de278b1403d0bb992b2d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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