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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:38:25+00:00 2026-05-17T15:38:25+00:00

I expecting the following to return true. public class HudsonJob { private String name;

  • 0

I expecting the following to return true.

   public class HudsonJob {

     private String name;
     private String status;
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public String getStatus() {
      return status;
     }
     public void setStatus(String status) {
      this.status = status;
     }

     public boolean equals(Object jobName) {
      return name.toLowerCase().equals(((String)jobName).toLowerCase());
     }

     public int hashCode() {
      return name.hashCode();
     }
    }

,

List<HudsonJob> existingJbsLst = hudsonUtil.getAllJobs(); // returns multiple HudsonJob objects in the list.

The statement I’m expecting to return true is :

boolean isExistingJob = existingJbsLst.contains("AnExistingJOB");
is always returning false.

OR
boolean isExistingJob = existingJbsLst.equals("AnExistingJOB"); is also returning false.

What should I add/change in the code to get the expected return value.

  • 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-17T15:38:26+00:00Added an answer on May 17, 2026 at 3:38 pm

    The “contains” operator loops through all the memers of the set, and compares the “want” value to the “found” value. I say that very carefully. If you say “joblist.contains(wantjob)”, this gives — to leave out some complexity, the relevant part:

    for (HudsonJob gotjob : joblist)
    {
      if (wantjob.equals(gotjob))
        return true;
    }
    

    That is, the comparison is “objectIAmLookingFor.equals(objectInList)”, NOT “objectInList.equals(objectIAmLookingFor)”.

    So when you search a list of HudsonJob’s for a String, it is using the String.equals function, NOT your HudsonJob.equals function. And String.equals knows nothing about HudsonJobs, so it promptly returns false.

    There are two ways to do what you want.

    One way is to change your HudsonJob.equals function to

    public boolean equals(Object o)
    {
      if (o==null || !(o instanceof HudsonJob))
        return false;
      return this.name.toLowerCase().equals(((HudsonJob)o).name.toLowerCase());
    }
    

    Then write

    HudsonJob wantjob=new HudsonJob();
    wantjob.setName("AnExistingJob");
    if (existingJobList.contains(wantjob))
      ... whatever ...
    

    The other way is to not use “contains” but instead write your own search function, like:

    public boolean jobInList(List<HudsonJob> existingJobs, String wantJob)
    {
      for (HudsonJob gotjob : existingJobs)
      {
        if (gotjob.name.toLowerCase().equals(wantJob.toLowerCase())
          return true;
      }
      return false;
    }
    

    (Actually if you need the toLowerCase it would be better to do the wantJob.toLowerCase before the loop, but whatever.)

    Then you can say

    if (jobInList(existingJobs,"AnExistingJob"))
    ... do something ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.