如何从地图中的特定键获取下一个键?

MrD*_*Duk 5 java dictionary key

我有类似的东西:

Map<Integer, String> topology = new TreeMap<Integer, String>();

其中包含例如:

01, my.vm.01.serv.com
04, my.vm.04.serv.com
07, my.vm.07.serv.com
08, my.vm.08.serv.com
09, my.vm.09.serv.com
Run Code Online (Sandbox Code Playgroud)

我希望能够说“我有钥匙07,请从这一点获取地图中的下一个钥匙”,理想情况下会返回08

有没有办法在不使用迭代器的情况下做到这一点?

Era*_*ran 3

用于topology.ceilingKey(7)获取下一个密钥。

请注意,这是接口的方法NavigableMap,因此您必须将TreeMap实例存储在NavigableMap变量中。

NavigableMap<Integer, String> topology = new TreeMap<Integer, String>();
Run Code Online (Sandbox Code Playgroud)