Java – how do I get the next key from a specific key in the map?
•
Java
I have something similar:
Map< Integer,String> topology = new TreeMap< Integer,String>();
These include, for example:
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
I want to be able to say "I have key 07. Give me the next key in the map from this point". Ideally, it will return to 08
Is there a way to do this without using iterators?
Solution
If you don't want to change the definition of map, you can try something similar
List<Integer> keys = new ArrayList<Integer>(topology.keySet()); Integer integer = keys.get((keys.indexOf(7))+1); // next key
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
二维码