Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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
-
donuts238484y@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 -
donuts238484yOr maybe something even more efficient that I haven't thought of... Hence why I ask.
-
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. -
donuts238484y@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...
-
Root826004yHash.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. -
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)
-
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. -
Root826004y@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! -
donuts238484y@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? -
donuts238484y
-
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.
-
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..
-
Guava has BiMap implementation.
Just be sure to be certain to double check the necessity of this Data Structure
Is there a Map like class in Java that has
getKey(value), getValue(key).
Guess not hard to implement but why reinvent the wheel..
rant