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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:43:06+00:00 2026-06-09T22:43:06+00:00

Why cannot I retrieve an element from a HashSet ? Consider my HashSet containing

  • 0

Why cannot I retrieve an element from a HashSet?

Consider my HashSet containing a list of MyHashObjects with their hashCode() and equals() methods overridden correctly. I was hoping to construct a MyHashObject myself, and set the relevant hash code properties to certain values.

I can query the HashSet to see if there "equivalent" objects in the set using the contains() method. So even though contains() returns true for the two objects, they may not be == true.

How come then there isn’t any get() method similar to how the contains() works?

What is the thinking behind this API decision?

  • 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-09T22:43:08+00:00Added an answer on June 9, 2026 at 10:43 pm

    If you know what element you want to retrieve, then you already have the element. The only question for a Set to answer, given an element, is whether it contains() it or not.

    If you want to iterator over the elements, just use a Set.iterator().

    It sounds like what you’re trying to do is designate a canonical element for an equivalence class of elements. You can use a Map<MyObject,MyObject> to do this. See this Stack Overflow question or this one for a discussion.

    If you are really determined to find an element that .equals() your original element with the constraint that you must use the HashSet, I think you’re stuck with iterating over it and checking equals() yourself. The API doesn’t let you grab something by its hash code. So you could do:

    MyObject findIfPresent(MyObject source, HashSet<MyObject> set)
    {
       if (set.contains(source)) {
          for (MyObject obj : set) {
            if (obj.equals(source))
              return obj;
          }
       }
    
      return null;
    }
    

    It is brute-force and O(n) ugly, but if that’s what you need to do…

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

Sidebar

Related Questions

While it is clear we cannot retrieve a list of installed applications on iOS,
I cannot retrieve data from Cassandra after I exit the cli. When I go
I have a really frustrating issue where I cannot retrieve any of the headers.
Retrieving stuff from the network cannot be done on the UI thread in android.
How do you read lines from a vector and compare their lengths? I have
trying to retrieve the id column of records that have their checkboxes selected in
Is there a way to retrieve the element source of an inline javaScript call?
Cannot seem to loop through the XElement and build a datatable. My XElement will
Cannot dump Stack Overflow XML file into SQL Server 2008. Could you please explain
Cannot find any example on Google.

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.