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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:26:59+00:00 2026-06-09T14:26:59+00:00

I have to add only unique elements to an ArrayList . How can I

  • 0

I have to add only unique elements to an ArrayList. How can I override the equals method for that purpose? I want something like:

public boolean equals (Object in) {
    if(in == null) {
        return false;
    }
    else if(in instanceof UniqueFruits) {
        // ?? check whether the newly added element exists or not           
        return true;
    }
    else {
        return false;
    }       
} 

How to check whether the newly added element exists or not? I have to check on the Fruit Names.

  • 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-09T14:27:01+00:00Added an answer on June 9, 2026 at 2:27 pm

    It sounds like you want to use a Set implementation. If order doesn’t matter, just use HashSet. If you want to keep insertion-order, use LinkedHashSet. Or, to maintain natural ordering, use TreeSet.

    In any case, make sure to override your element object’s equals method. What you have is a good start. After checking in instanceof UniqueFruits, cast in to UniqueFruits:

    UniqueFruits uf = (UniqueFruits)in;
    

    You can then check each relevant field using equals in turn (make sure to check for null first if a field is nullable). Any modern IDE will help you generate equals automatically. It may be educational to try it yourself first, then compare with the generated version.

    Make sure to override hashCode also (IDEs will similarly help you with this, and there is plenty of reading online about the matter – just search).

    If you use TreeSet (or some other SortedSet implementation), your element object should either implement Comparable or else you should provide the SortedSet with a Comparator.

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

Sidebar

Related Questions

In Java is there an object like a Set that can contain only unique
I have a several processes node.js. I want to only add one string to
I have few nested DIVs at page. I want to add event only for
I have an ordered list in HTML. I would like to add styling only
I have two JPanels that needs to add a JPanel into it, however only
I'd like to add a keyword to my language. This keyword would only have
On a customer website, I have to add a background image for only a
I have an outlook add-in which runs only on the first startup of Outlook,
I have only used MapRoute and slightly wondering where Routes.Add is used and on
I have an add this button on the website that is rendering a border

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.