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

I need a map, but when I call get (key, n), it should not only return all records with search key values, but also return all records with the same n last significant bits of the key as the search key (for example, apply things such as key & (1 < < (n 1) - 1)) Has such a thing been implemented in Java?

Solution

Not exactly, but you can use navigablemap Submap to achieve this for example

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 search based on the lowest bit instead of the highest bit, you must reverse these bits before adding and searching This combines the lowest, second and third low positions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>