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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:15:22+00:00 2026-05-31T18:15:22+00:00

I have a list of items, where each item is a simple class containing

  • 0

I have a list of items, where each item is a simple class containing 2 public strings.
I have an equals method that simply makes use of the equalsIgnoreCase methot of String for both strings.

public class data
{
    public String a;
    public String b;

    public boolean equals(data d)
    {
        if(a.equalsIgnoreCase(d.a) && b.equalsIgnoreCase(d.b))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

I want to be able to remove an element even if it’s not the same instance of the one in the list but equal to it.

Right now I’m doing this:

public void remove(data dataToRemove)
{
    for(data i : _list)
    {
        if(i.equals(dataToRemove))
        {
            _list.remove(i);
            break;
        }
    }
}

Is there a better way to do this?

  • 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-31T18:15:23+00:00Added an answer on May 31, 2026 at 6:15 pm

    A few comments:

    • Your equals method does not override the equals method of Object (argument should be of Object type, not data type).
    • You should improve your equals method to account for nulls etc.
    • And finally, you should override hashcode() too when you override equals() – if not you might encounter some weird behaviors when using Sets or Maps for example.

    If you properly override the equals method, you can then just use the remove method.
    See below the auto generated equals and hashcode generated by Netbeans, amended to use the equalsIgnoreCase method.

    public static void main(String[] args) {
        List<Data> list = new ArrayList<Data>();
        list.add(new Data("a", "b"));
        list.add(new Data("a", "c"));
        System.out.println(list.size()); //2
        list.remove(new Data("A", "b"));
        System.out.println(list.size()); //1
    }
    
    public static class Data {
    
        public String a;
        public String b;
    
        public Data(String a, String b) {
            this.a = a;
            this.b = b;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj == null) return false;
            if (getClass() != obj.getClass()) return false;
            final Data other = (Data) obj;
            boolean sameA = (this.a == other.a) || (this.a != null && this.a.equalsIgnoreCase(other.a));
            if (!sameA) return false;
            boolean sameB = (this.b == other.b) || (this.b != null && this.b.equalsIgnoreCase(other.b));
            if (!sameB) return false;
            return true;
        }
    
        @Override
        public int hashCode() {
            int hash = 3;
            hash = 89 * hash + (this.a == null ? 0 :this.a.toUpperCase().hashCode());
            hash = 89 * hash + (this.b == null ? 0 : this.b.toUpperCase().hashCode());
            return hash;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this is simple: I have a relatively long list where each list item
I have a list of items. When I create the list each item has
Say I have a list of items from a database. Next to each item
I have a list of data that is a schedule. Each item has a
I have a simple Smart TV app that shows a list of items in
I have a very simple list of objects that represt people. Each object has
I'm developing a List of four items. Each item needs to have a top
I have a list, and each item is linked, is there a way I
I have Collection List<Car> . How to compare each item from this collection with
I have a hard-coded list, each list item of which has an ID. I

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.