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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:55:34+00:00 2026-05-26T13:55:34+00:00

I want to implement a collection (a bit like a map but for single

  • 0

I want to implement a collection (a bit like a map but for single values rather than pairs) that performs a hash when searching its contents. I’m trying to make my programs really effcient. Currently the program uses a HashMap and adds the primary key from a database to it twice:

HashMap<Long, Long> userKeys = new HashMap<Long, Long>();
Some operations
userKeys.add(key, key);

Is there a Hash or Map I can use to avoid adding the key twice? As it’s a primary key in a database I know there are going to be no duplicates and hence the reason I thought of a hash to be efficient.

I thought a Vector might be a good choice but when I looked at the contains() method it goes through every entry which is going to be very inefficient in this case.

I’ve also had a look at HashSet’s but unfortunately from what I’ve seen there’s no method for extracting values so that they can be used, unlike the HashMaps.get(Object) method.

Desired set up:

Collection<Long> userKeys = new Collection<Long>();
Some operations
userKeys.add(key);

Desired operations:

userKeys.contains(key) <- Uses hash here

userKeys.get(index)

  • 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-26T13:55:34+00:00Added an answer on May 26, 2026 at 1:55 pm

    I think HashSet is what you should be using – if you don’t really have a key/value relationship, it should be all you need. You can get at the values by iterating over the set.

    It’s not clear what your get(index) is meant to return… what’s the index here?

    If you need to preserve insertion order, you could use LinkedHashSet.

    If you need to preserve insertion order and have random access by insertion order (i.e. get the third entry you added via get(2)) then you could keep a HashSet and an ArrayList, keeping the two in sync yourself. (Encapsulate them in a separate type.) Use the HashSet for containment tests, and the ArrayList for random access.

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

Sidebar

Related Questions

I want to use List.Find() on a simple collection that does not implement Find().
I want to implement a keyed observable collection in Silverlight, that will store unique
I was told by a rather smart person that you cannot implement garbage collection
I want to implement a custom collection that contains instances of my class. This
I want implement in my software solution an VBA editor but in c# 3.0.
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
Issue I want to implement TransparencyChecker Interface which would verify that from any exchange
I am new to generics. I want to implement my own collection by deriving
I want to implement an ISAPI filter like feature using HttpModule in IIS7 running
http://www.telerik.com/purchase/faqs/telerik-premium-collection-for-dotnet.aspx I want to implement similarly. Any ideas? Thanks.

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.