Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Home_work_All_Collections {
- public static void main(String[] args) {
- User user = new User("C", 6, true);
- User user1 = new User("D", 7, true);
- LinkedList<User> list = new LinkedList<>();
- list.add(user);
- list.add(user1);
- for (User list1 : list) {
- System.out.println(list1);
- }
- }
- }
- import java.util.Objects;
- public class User implements Comparable<User> {
- private String name;
- private int age;
- private boolean activated;
- public User() {
- }
- public User(String name, int age, boolean activated) {
- this.name = name;
- this.age = age;
- this.activated = activated;
- }
- public boolean isActivated() {
- return activated;
- }
- public void setActivated(boolean activated) {
- this.activated = activated;
- }
- 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;
- }
- @Override
- public int hashCode() {
- int hash = 7;
- hash = 79 * hash + Objects.hashCode(this.name);
- hash = 79 * hash + this.age;
- hash = 79 * hash + (this.activated ? 1 : 0);
- return hash;
- }
- @Override
- public boolean equals(Object obj) {
- 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 (this.age != other.age) {
- return false;
- }
- if (this.activated != other.activated) {
- return false;
- }
- return true;
- }
- @Override
- public String toString() {
- return "User{" + "name=" + name + ", age=" + age + ", activated=" + activated + '}';
- }
- @Override
- public int compareTo(User o) {
- int res = name.compareTo(o.getName());
- if (res == 0) {
- if (age < o.getAge()) {
- return -1;
- }
- if (age > o.getAge()) {
- return 1;
- }if(age == 0){
- if(activated == true){
- return 1;
- }else
- return -1;
- }
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement