Advertisement
Tsuki11

Untitled

Jun 20th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. package E7_EqualityLogicc;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.HashSet;
  7. import java.util.Set;
  8. import java.util.TreeSet;
  9.  
  10. public class Main {
  11. public static void main(String[] args) throws IOException {
  12. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13.  
  14. int countInput = Integer.parseInt(reader.readLine());
  15.  
  16. Set<Person> treeSet = new TreeSet<>();
  17. Set<Person> hashSet = new HashSet<>();
  18.  
  19. for (int i = 0; i < countInput; i++) {
  20. String[] tokens = reader.readLine().split("\\s+");
  21. Person person = new Person(tokens[0],Integer.parseInt(tokens[1]));
  22. treeSet.add(person);
  23. hashSet.add(person);
  24. }
  25.  
  26. System.out.println(treeSet.size());
  27. System.out.println(hashSet.size());
  28.  
  29. }
  30. }
  31.  
  32.  
  33. package E7_EqualityLogicc;
  34.  
  35. import java.util.Comparator;
  36.  
  37. public class PersonComparator implements Comparator<Person> {
  38. @Override
  39. public int compare(Person first, Person second) {
  40. return first.getAge() - second.getAge();
  41. }
  42. }
  43. package E7_EqualityLogicc;
  44.  
  45. import java.util.Comparator;
  46.  
  47. public class Person implements Comparable<Person> {
  48. private String name;
  49. private int age;
  50.  
  51. public void setName(String name) {
  52. this.name = name;
  53. }
  54.  
  55. public void setAge(int age) {
  56. this.age = age;
  57. }
  58.  
  59. public Person(String name, int age) {
  60. this.setName(name);
  61. this.setAge(age);
  62. }
  63.  
  64. public String getName() {
  65. return this.name;
  66. }
  67.  
  68. public int getAge() {
  69. return this.age;
  70. }
  71.  
  72.  
  73. @Override
  74. public boolean equals(Object other) {
  75. /*
  76. Метода по default;
  77.  
  78. if (this == other) { // стрингове по референция, паметта им еднаква ли е. текущ обект с дадения;
  79. return true;
  80. }
  81. if (other == null || getClass() != other.getClass()) { //ако подадения е със стойност нул или текущия обект
  82. е различен от подадения връщаме false, по реф. на класовете
  83. //може и other.getClass.getSimpleName().equals(Person.class.getSimpleName()) по имена, по стрингове
  84. return false;
  85. }
  86. Person person = (Person) other; // сигурни сме, че other Object е от Person след проверките - кастване, за да достъпим клас членовете
  87. return age == person.age && // ако годините и имената са равни връщаме true;
  88. Objects.equals(name, person.name);
  89.  
  90. */
  91.  
  92. if (this.hashCode() == other.hashCode()) {
  93. return true;
  94. }
  95. if (other.getClass().getSimpleName().equals(Person.class.getSimpleName())) {
  96. Person current = (Person) other;
  97. return this.name.equals(current.getName()) && this.age == current.getAge();
  98. }
  99. return false;
  100. }
  101.  
  102. @Override
  103. public int hashCode() {
  104. int hash = 7;
  105. hash = 31 * hash + age;
  106. hash = 31 * hash + (name == null ? 0 : name.hashCode());
  107. return hash;
  108.  
  109. //return Objects.hash(name, age); default method
  110.  
  111. // @Override
  112. // public int hashCode() {
  113. // return this.getName().hashCode() + Integer.hashCode(this.getAge()) * 31;
  114. // }
  115. }
  116.  
  117. @Override
  118. public int compareTo(Person other) {
  119. return Comparator.comparing(Person::getName).thenComparing(Person::getAge).compare(this, other);
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement