How do you compare the object to the HashSet collection?



  • hashCode and equals changed. The user introduces login and passwords from the consoles on which the user facility is created and the facility is compared to the HashSet sites, after which the message is to the console. Also interested is the question of how to correctly redefin the method of equals?

    public class MainClass {
    

    public static void main(String[] args) {
    User user1 = new User("mike", "112233");
    User user2 = new User("jim", "223344");
    User user3 = new User("tommy", "334455");
    User user4 = new User("veronika", "556677");
    User user5 = new User("eddy", "778899");
    Authentical authentical = new Authentical(user1, user2, user3, user4, user5);
    try (Scanner scan = new Scanner(System.in)) {
    String login = scan.nextLine();
    String password = scan.nextLine();
    authentical.auth(login, password);
    }
    }

    }

    public class Authentical {
    Set<User> hset = new HashSet<User>();

    public Authentical(User user1, User user2, User user3, User user4, User user5){
    hset.add(user1);
    hset.add(user2);
    hset.add(user3);
    hset.add(user4);
    hset.add(user5);
    for(User u:hset){
    System.out.println(u);
    }
    }
    public boolean auth(String login,String password){
    User user = new User(login, password);
    Iterator<User> iterator = hset.iterator();
    while(iterator.hasNext()){
    if(iterator.equals(user)){
    System.out.println("Пользователь с таким логином и паролем зарегистрирован");
    return true;
    }
    }
    System.out.println("Пользователь не найден");
    return false;
    }
    }

    public class User {
    private String login;
    private String password;
    private int id = 17;

    public User(String login, String password){
    this.login = login;
    this.password = password;
    }

    @Override
    public int hashCode(){
    return 37 * id + login.hashCode() + password.hashCode();
    }

    //упрощенный equals
    public boolean equals(User user) {
    return (this.login==user.login)&&(this.password==user.password);
    }
    }



  • http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals%28java.lang.Object%29 in the design of the method equals:

    1. Reflexiveness: for any non-null objects x x.equals(x) must return true
    2. Symmetry: for any non-null objects x and y, x.equals(y) must return trueif and only if y.equals(x) returns true
    3. Transit: for any non-null objects x, y, and z, if x.equals(y) returns true and y.equals(z) returns trueThen x.equals(z) must return true
    4. Continuity: repeated challenge equals() shall return the same value until any value of the object &apos; s properties has been changed. If two objects are equal, they will be equal as long as their properties remain unchanged.
    5. For any non-null objects x, x.equals(null) must return false

    In method equals You're comparing references to objects, not their contents. Also draw attention to the signature of the method equals♪ As an argument, a type object is accepted Object♪ It'll be a good thing:

    @Override
    public boolean equals(Object obj) {
         if (this == obj) {
             return true;
         }
    
     if (obj == null) {
         return false;
     }
    
     if (!(obj instanceof User)) {
         return false;
     }
     User otherUser = (User) obj;
    
     return (login.equals(otherUser.login)) &amp;&amp; (password.equals(otherUser.password));
    

    }

    Also to search the site Set a standard method can be used http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#contains%28java.lang.Object%29




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2