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

  • Home
  • SEARCH
  • 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 6883547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:23:49+00:00 2026-05-27T05:23:49+00:00

I have some object class A { private Long id; private String name; public

  • 0

I have some object

class A {
  private Long id; 
  private String name; 
  public boolean equals(Long v) {
     return this.id.equals(v);
  }
}

and ArrayList of these objects.
What I want is to be able to check if that list contains some object by object’s field.
For example:

ArrayList<A> list = new ArrayList<A>(); if (list.contains(0L)) {...}

but overrided Equals method is not helps me. What I am doing wrong?
Thank you

UPDATE
And should I override a hashcode() method too?

  • 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-05-27T05:23:49+00:00Added an answer on May 27, 2026 at 5:23 am

    here’s some code that might demonstrate how it works out:

    import java.util.ArrayList;
    
    class A {
      private Long id; 
      private String name; 
    
      A(Long id){
          this.id = id;
      }
    
        @Override
      public boolean equals(Object v) {
            boolean retVal = false;
    
            if (v instanceof A){
                A ptr = (A) v;
                retVal = ptr.id.longValue() == this.id;
            }
    
         return retVal;
      }
    
        @Override
        public int hashCode() {
            int hash = 7;
            hash = 17 * hash + (this.id != null ? this.id.hashCode() : 0);
            return hash;
        }
    }
    
    public class ArrayList_recap {
        public static void main(String[] args) {
            ArrayList<A> list = new ArrayList<A>(); 
    
            list.add(new A(0L));
            list.add(new A(1L));
    
            if (list.contains(new A(0L)))
            {
                System.out.println("Equal");
            }
            else
            {
                System.out.println("Nah.");
            }    
        }
    
    }
    

    First, there is an override of the equals(Object o) method. Then there is the override of the hashCode() as well. Also note that the instanceof A check in the equals will ensure that you’re not trying to compare different objects.

    That should do the trick! Hope it helped! Cheers 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a structure like: class SomeObject Public Name as String Public Created
Basically I have some class objects, each with three properties. Once one class object
Assume some domain and view objects (that have no common base class) public class
Assume we have following JPA Entities: class Parent { @Id private Long id; }
I have a command object associated with a spring form controller: public class PluginInstance
I have just refactored a colleague's code that, roughly, looked like this... public class
I'm working on a class-based php web app. I have some places where objects
I have a class with a vector of pointers to objects. I've introduced some
I have some object that is instantiated in code behind, for instance, the XAML
I need to have some object hanging around between two events I'm interested in:

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.