Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7750165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:13:14+00:00 2026-06-01T11:13:14+00:00

This is a question about how to implement the equals method when I need

  • 0

This is a question about how to implement the equals method when I need to find instance of the object in a List given a value that one of the instances my have in their member.

I have an object where I’ve implemented equals:

class User {

    private String id;

    public User(id) {
        this.id = id;
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof User)) {
            return false;
        }
        return ((User)obj).id.equals(this.id);
    }
}

Now if I want to find something in the List I would do something like this:

public function userExists(String id) {
        List<Users> users = getAllUsers(); 
        return users.contains(new User(id));
}

But perhaps this might be a better implementation?

class User {

    private String id;

    public boolean equals(Object obj) {
        if (!(obj instanceof User)) {
            return false;
        }
        if (obj instanceof String) {
            return ((String)obj).equals(this.id);
        }
        return ((User)obj).id.equals(this.id);
    }
}

With this instead:

public function userExists(String id) {
    List<Users> users = getAllUsers(); 
    return users.contains(id);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:13:16+00:00Added an answer on June 1, 2026 at 11:13 am

    Do not override equals for things that are not mathematically equal.

    You might think it is a good idea to do

    User bob = new User("Bob");
    if (bob.equals("Bob")) {
      ...
    }
    

    but it rarely is. Do you want all of the equals observing code getting confused when Strings are “equal” to Users?

    If you want a lookup method, write it

    class User {
    
        private String id;
    
        public boolean equals(Object obj) {
            if (obj instanceof User) {
                User other = (User)obj;
                if (id.equals(other.id)) {
                  return true;
                }
            }
            return false;
        }
    
        public String getId() {
            return id;
        }
    
    }
    

    Then the code elsewhere to maintain the “fast lookup” table.

    Map<String, User> idTable = new HashMap<String, User>();
    User bob = new User("Bob");
    idTable.put(bob.getId(), bob);
    
    public User findUser(String id) {
      return idTable.get(id);
    }
    

    Note that this doesn’t mess around with the equals implementation, so now you can safely have Sets of Users, Lists of Users, etc. all without worrying if somehow a String will foul the works.

    Now if you can’t find a good place to maintain a Map of Users indexed by their id, you can always use the slower Iterator solution

    List<User> users = new List<User>();
    users.add(new User("Bob"));
    users.add(new User("Steve"));
    users.ass(new User("Ann"));
    
    public User findUser(String id) {
      Iterator<User> index = users.iterator();
      while (index.hasNext()) {
        User user = index.next();
        if (id.equals(user.getId())) {
          return user;
        }
      }
      return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a general design question about how to make a web application that
This was originally going to be a question about how to implement this because
Note This is not a question about how to implement or emulate duck typing
I've asked a question about this class before, but here is one again. I've
I'm revisiting and re-implementing the code that caused me to ask this question about
Short question: How do I get the object.GetHashCode() value for an object that has
This is not a question about jQuery, but about how jQuery implements such a
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
This question is a follow up to my previous question about getting the HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.