Is there any way to loop in NavigableMap in Java? I want to access all of item in NavigableMap.
The same way you would loop any collection, with an iterator or for-each loop.
NavigableMap<K, V> map = ...
for(K key: map.keySet()) // iterate keys.
for(V value: map.values()) // iterate values.
for(Entry<K, V> entry: map.entrySet()) // iterate key/value entries.
A NavigableMap is a Map . You get all its keys using keySet()
, all its values using values()
, and all its entries using entrySet()
.
Since NavigableMap
extends Map
it should still provide the methods values()
, keySet()
and entrySet()
. Use those to iterate over the entries/values/keys as you'd do with any other map.
Correct advance looping for Map should like this->
for(Map.Entry entry: map.entrySet()) // iterate key/value entries.
If you only to get all of the items, I think you don't need use loop,just print these.
NavigableMap<String, Integer> nav = new TreeMap<String, Integer>();
nav.put("key1", value1);
nav.put("key2", value2);
nav.put("key3", value3);
System.out.printf("The Whole:%s", nav);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.