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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:43:07+00:00 2026-05-23T11:43:07+00:00

I implemented a unique map. It’s a hashmap which is bi-directional, where not only

  • 0

I implemented a unique map. It’s a hashmap which is bi-directional, where not only keys are unique but values too.

public interface UniqueMap<K,V>{

    V uniquePut(K key, V value);

    UniqueMap<V,K> inverse(); 
}

This is a possible implementation:

public class SimpleUniqueMap<K,V> implements UniqueMap<K,V>, Iterable<K>{

    public HashMap<K,V> uniqueMap = new HashMap<K,V>();

    class EnumSimpleUniqueMap implements Iterator<K>{

        int count = uniqueMap.size();

        public boolean hasNext(){
            return count > 0;
        }

        public K next(){
            if(count == 0){
                throw new NoSuchElementException();     
            }else{
                count--;
                //...
            }
        }

        public void remove(){
            throw new UnsupportedOperationException();
        }
    }

    public Iterator<V> iterator(){
        return new EnumSimpleUniqueMap();
    }

    public V uniquePut(K key, V value){ 
        return null;
    }

    public UniqueMap<V,K> inverse(){
        return null;
    }
}

As you can see I already tried to implement an iterator for my unique map. But from a hashmap values are not accessed by position but by key. So usually I would take the counter and access values but in this case it is not possibe.

Actually it would be enough to iterate over the keys and retrieve them one by another. How can I do that? Is there a way to retrieve some kind of entry object containing both key and value?

I know that I can retrieve the iterator from an map object but this is not an option for me.

  • 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-23T11:43:08+00:00Added an answer on May 23, 2026 at 11:43 am

    UPDATE: Most simple of all, use

    org.apache.commons.collections.BidiMap
    

    But if your really want to roll your own, then consider this:

    Usually, Maps don’t implement Iterable. In your case, you can get the Iterator for free by calling any of these

    map.keys().iterator(); // is the same as
    map.inverse().values().iterator();
    
    map.values().iterator(); // is the same as
    map.inverse().keys().iterator();
    
    map.entrySet().iterator(); // almost the same as
    map.inverse().entrySet().iterator();
    

    on your map, depending on what you want to iterate over. For that, you’d have to make

    public interface UniqueMap<K,V> extends Map<K, V> {
        // no need for uniquePut(), you already have Map.put()
        UniqueMap<V,K> inverse(); 
    }
    

    It’s also a good idea to make your implementation extend

    java.util.AbstractMap<K, V>
    

    Which has a lot of base functionality for maps already.

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

Sidebar

Related Questions

Is it right by saying it's common to have some interface members not implemented
I have a map which contains integer values. i want to re-arange this map
Several months ago I implemented a solution to choose unique values from a range
Implemented the Sink Class - to receive event notifications from COM Server Event Interface
Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>
I implemented a small OOP library in Lua, and two things are not quite
Having implemented CLogClass to make decent logging I also defined macro, but it works
I have to implement map values in my Grails app. I have a class
I have a vector class with hashCode() implemented. It wasn't written by me, but
I am searching for a (multi)map where there values are associated by different key

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.