Is there a variable key length map in the Java world?

I need a map, but when I call get (key, n), it should return not only all records with the desired key value, but also all where the n last significant bits of the key match the search (for example, using something like the & key (1 <(n + 1) -1)).

Is there something similar in Java?

+6
source share
2 answers

Not really, but you can use NavigableMap.subMap to implement this. eg.

NavigableMap<Integer, Value> map = int keyBase = key & ~((1 << n)-1); Map<Integer, Value> subMap = map.subMap(keyBase, true, keyBase + (1 << n), false); 

If you want to perform a search based on the low bits instead of the high bits, you need to cancel the bit before adding and searching. This combines the least significant bit, the second least significant bit, the third least significant bit, etc.

+10
source

HashMap is not going to do this, but TreeMap can.

You will need to normalize and cancel your keys (i.e. decide how many bits you want to keep, and change the bits to make the less significant bits the most important). You can then separate the less significant bits (previously the most significant bits) from your keys and use the range search on the tree map to find the answer.

+2
source

Source: https://habr.com/ru/post/906106/


All Articles