5
donuts
3y

Is there a Map like class in Java that has

getKey(value), getValue(key).

Guess not hard to implement but why reinvent the wheel..

Comments
  • 3
    The issue is that values don’t have to be unique. You could have a->b c->b where a and c are keys and b is a value
  • 2
    @d-fanelli well yea that would need to be a given but im my case it's a 1-1 relationship.

    The class obviously at insertion would check this.

    Underlying implementation would be 2 maps + the duplicate check
  • 1
    Or maybe something even more efficient that I haven't thought of... Hence why I ask.
  • 2
    If space is not an issue you could just make two maps, one from keys to values and the other one going the other way.

    Yes it's redundant but you're trading off space for speed.

    It'll work because you know it's a 1-1 relationship, which a map doesn't understand.
  • 1
    @RememberMe yes that's what I said but wondering if the like a genius algo way that only rely smart algo ppl could think of...
  • 2
    Hash.get_key(value) could return the first key with matching value, or an array of keys.

    The former is speedy; the latter is more flexible but slower.
  • 2
    I’m not sure there’s any other way. Not even Root could give you a mind twisting solution. I’m too exhausted from the gym to even do shit now lol (arrgh im so out of shape, can’t wait to get back into it, damn holidays making me lazy)
  • 1
    I mean, there's HashMap that already has .get(key).

    A method for looking for a key based on value would be

    static Object getKey(Object value, HashMap m) {

    for(Object key : m.keySet()) {
    if(m.get(key).equals(value)) {
    return key;
    }
    }

    return null;
    }

    Correct me if I'm wrong I'm writing this at 3:56 (not anymore, now it's 3:58) AM and if course you could generify this and implement it in your own HashMap thing.
  • 3
    @d-fanelli Lies. Designing a Hash.keys(value) that works in O(constant) time would be easy: make a new Hash for each duplicate value, and store it on the original class instance in an array. Iterate over the array, pull out the values, and you’re done.

    Hashes would require unique values for both keys and values, but you’d hide this by automatically maintaining the hashes in the setter.

    This honestly feels too weird and contrived to be a good idea, however. But possible and mind-twisting and speedy? Absolutely!
  • 1
    @Root isn't that how hash maps work though?

    If the hashing function returns the same value for a 2 diff keys, it will put the key-value in the next free spot in an array start at x (whatever index the hashing function returns).

    Get(key) starts also calls it for X and starts from there and keeps iterating until it find the matching key?
  • 1
    @donuts Possibly. I’ve never implemented one before.
  • 2
    @Root https://cs.usfca.edu/~galles/...

    Insert A, M, Z.

    Actually used a linkedlist but similar idea
  • 2
    I don’t quite understand what’s wrong with using two hashes for key to value and value to key. When you call hashObj.add(key,value) the add function will check if value is a key in the value to key hash. If it is, return “no duplicates, dumbass” If not, then you win. Everything will be O(1), the only issue is extra space for a chunky 2nd hashmap.
  • 1
    I’d say the easiest way is to use a hashmap and use lambda to filter by value. Not a method but a till gonna be a one liner I guess..
  • 1
    Guava has BiMap implementation.
    Just be sure to be certain to double check the necessity of this Data Structure
Add Comment