You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.6 KiB
68 lines
1.6 KiB
/* |
|
* To change this license header, choose License Headers in Project Properties. |
|
* To change this template file, choose Tools | Templates |
|
* and open the template in the editor. |
|
*/ |
|
package collectionexample2; |
|
|
|
import java.util.Objects; |
|
|
|
/** |
|
* |
|
* @author denis |
|
*/ |
|
public class User implements Comparable<User>{ |
|
String name; |
|
Integer age; |
|
|
|
public User(String name, Integer age) { |
|
this.name = name; |
|
this.age = age; |
|
} |
|
|
|
@Override |
|
public String toString() { |
|
return "User{" + "name=" + name + ", age=" + age + '}'; |
|
} |
|
|
|
@Override |
|
public int hashCode() { |
|
int hash = 7; |
|
hash = 37 * hash + Objects.hashCode(this.name); |
|
hash = 37 * hash + Objects.hashCode(this.age); |
|
return hash; |
|
} |
|
|
|
@Override |
|
public boolean equals(Object obj) { |
|
if (this == obj) { |
|
return true; |
|
} |
|
if (obj == null) { |
|
return false; |
|
} |
|
if (getClass() != obj.getClass()) { |
|
return false; |
|
} |
|
final User other = (User) obj; |
|
if (!Objects.equals(this.name, other.name)) { |
|
return false; |
|
} |
|
if (!Objects.equals(this.age, other.age)) { |
|
return false; |
|
} |
|
if (!Objects.equals(this.hashCode(), other.hashCode())) { |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
@Override |
|
public int compareTo(User o) { |
|
int res = this.name.compareTo(o.name); |
|
if(res == 0) res = this.age.compareTo(o.age); |
|
return res; |
|
} |
|
|
|
|
|
}
|
|
|