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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:23:13+00:00 2026-06-06T06:23:13+00:00

This question was initially misphrased, see the EDIT below. I’ll leave it up for

  • 0

This question was initially misphrased, see the EDIT below. I’ll leave it up for context.

I’ve been thinking about smart ways to build a bijective (i.e. one-to-one) mapping. Mapping a function A->B (many-to-one) is basically what HashMap(A,B) does. If I now wanted to have a data structure that implements something one-to-one with contains() in O(1), would there be something in the java standard libraries that I could use? Mind you, I don’t need this for anything right now, this was just something I thought about recently and couldn’t come up with a data structure for, so answers aren’t a rush. Is there a class like that? If not, what do you think why that is?

All I could find on SO are things about hibernate, that was of no help to me.

EDIT:
My question was ill phrased, so some explanation is due.

What I meant was is the “backward” mapping B->A. HashMap(A,B) has contains(A) and contains(B) both in O(1), so that’s not even what I meant, sorry for the confusion. What I meant was, is there a datastructure mapping A<->B that has getValue(A) and getKey(B) in O(1)?

I realize this could be done with two HashMaps (A,B) and (B,A) that are maintained to contain the same relation, but I feel there should be one data structure that handles that without having to do it “manually”.

  • 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-06T06:23:14+00:00Added an answer on June 6, 2026 at 6:23 am

    I don’t know of an existing class that does O(1) for both containsKey and containsValue, but you can do it by extending HashMap so that on insert, you add each value to an internal HashSet. Overloading containsValue to do a lookup on the values HashSet. The standard HashMap has O(1) containsKey, but O(n) containsValue.

    Likewise, you can enforce 1:1 in the insert and checking for existing values.

    Note that if you get a ton of collisions the HashSet lookup can get to O(n) in the worst case.

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

Sidebar

Related Questions

EDIT: This question was initially too general, I think. So What I really need
This question was initially posted on Google Moderator for the AndroidDev Office Hours Hangout
This is a related question to one I posted earlier today, I was initially
please see this question. The initial question was solved, and I have a method
This question is about tidying up code and better management of said code butI'm
Danny initially asked this question in response to a recent Scott Hanselman post: Who
Initially I had the issue reported in this question . Now, what I noticed
This question seems to have been asked many times in many different forms but
This is a question about Application design rather than how-to-do specific actions. I am
I know this question has been asked a number of times. I seem to

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.