Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the next key, from a specific key in a map?

I have something like:

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

Which contains 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'd like to be able to say "I have key 07, get me the next key in the map from this point", which would ideally return 08.

Is there a way to do this without using an iterator?

like image 962
MrDuk Avatar asked Mar 15 '23 08:03

MrDuk


1 Answers

"I have key 07, get me the next key in the map from this point",

If you do not want to change the definition of Map, you could try something like

List<Integer> keys = new ArrayList<Integer>(topology.keySet());
Integer integer = keys.get((keys.indexOf(7))+1); // next key
like image 80
Suresh Atta Avatar answered Mar 28 '23 23:03

Suresh Atta