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 7195157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:31:20+00:00 2026-05-28T20:31:20+00:00

this is am learning java now. I just want to know whether should we

  • 0

this is am learning java now. I just want to know whether should we write any comparator for comparing the list of objects with a particular object? And i also want to know why the particular line is not comparing the list

myList.contains(obj1)

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class createItem {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String [] itemid = null;

        try{
            File itemFile =  new File("C:\\"+"Sano"+".xml");
            itemFile.createNewFile();

            List<ObjectIdName> myList = new ArrayList<ObjectIdName>();
            ObjectIdName obj1 = new ObjectIdName("ABC","ABC");

            myList.add(new ObjectIdName("DEF","DEF"));
            myList.add(new ObjectIdName("ABC","ABC"));
            if(myList.contains(obj1)){
                System.out.println("Has");

            }
            System.out.println("MyList:" + myList.size());

        }           
        catch(Exception e){
            e.printStackTrace();
        }
    }


}
class ObjectIdName implements Serializable{

    private static final long serialVersionUID = 1L;

    private String id;

    private String name ;

    public ObjectIdName (String id, String name) {
        this.id= id;
        this.name =name;

    }
}
  • 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-28T20:31:21+00:00Added an answer on May 28, 2026 at 8:31 pm

    By default, equals() compares the references:

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    Since obj1 and the second object you insert into the list are two distinct objects, myList.contains(obj1) returns false.

    To compare the fields, ObjectIdName has to implements its own equals() method.

    Note that if you implement equals(), it is also a good practice to implement hashCode() (even if it’s not used by your code).

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

Sidebar

Related Questions

Hey everyone. I am an experienced java programmer and am just learning C++. Now
I am learning Scala now and I want to write some silly little app
I'm learning java and now I have this question. I created a class named
I am beginning in java (I'm learning in microedition) and I got this error:
I have been learning python for some time now. While starting this learning python
I'm learning this right now so the questions might be a little juvenile. Here's
I'm working on a project in C#, it's about E-learning.This project should use plug-ins
I knew C, Cpp, Java, Servlets, JSP and Objective C. Now, i want to
I am learning java ee right now. I have successfully implemented a hello-world style
I just began learning Java. My friend who knows much more than me has

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.