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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:38:27+00:00 2026-06-12T14:38:27+00:00

How to Store unique objects to avoid the duplicates in java Set? For example

  • 0

How to Store unique objects to avoid the duplicates in java Set?

For example

Consider Employee object which (Employee Id, name, salary….)

list of employee of objects need to add in the Set.
We need to restrict the Set for the duplicate elements which need to identify by the “Employee Id.

What are the best way’s to do?

  • 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-12T14:38:28+00:00Added an answer on June 12, 2026 at 2:38 pm

    If you are using an implementation of a java.util.Set, it should not allow duplicates as long as your equals and hashCode methods are implemented properly. Not sure why you have hashmap and hashtable as tags on your question though. Maybe you should rephrase your question and add the code that gives you issues?

    Edit: considering your edit:

    If you use a Set, your Employee should have the following methods:

    
        @Override
        public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((id == null) ? 0 : id.hashCode());
          return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
              return true;
            if (obj == null)
              return false;
            if (getClass() != obj.getClass())
              return false;
            Employee other = (Employee) obj;
            if (id == null) {
              if (other.id != null)
                return false;
            } else if (!id.equals(other.id))
              return false;
            return true;
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a class that will store a unique object elements. I
I have an array of a structure which is supposed to store unique character
I am currently using a Hashtable to store a list of unique identifiers and
I want to store a list of objects in an std::vector , but the
I need a collection in which i can store heap-allocated objects having virtual functions.
Ive got a large bunch of objects (potentially 1000's) which I need to store
first, an example: given a bunch of Person objects with various attributes (name, ssn,
I have (lots of) objects Foo with an unique ID and want to store
If I have a list of objects, for example List<Cake> , is it possible
I will have alot of similar objects with similar parameters. Example of an object

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.