Code example for NavigableMap

Methods: containsKey, get, higherKey, lowerKey, subMap

0
package demo.cache; 
 
import java.util.NavigableMap; 
import java.util.TreeMap; 
 
public class NavigableSetSample { 
    public NavigableSetSample() { 
        NavigableMap<String, String> dic = init();
        String key = "010";
        if (dic.containsKey(key)) {
            System.out.printf("%s 的城市是: %s%n", key, dic.get(key));
        } else { 
            String lowerKey = dic.lowerKey(key);
            String higherKey = dic.higherKey(key);
            System.out.printf("%s 区号相近的城市是: %n", key);
            if (lowerKey != null) {
                System.out.printf("%s(%s)%n", dic.get(lowerKey), lowerKey);
            } 
            if (higherKey != null) {
                System.out.printf("%s(%s)%n", dic.get(higherKey), higherKey);
            } 
        } 
        
        NavigableMap<String, String> subdic
            = dic.subMap("020", true, "029", true);
  
        System.out.printf("%n02X 区号的城市是:%n%s", subdic);
    } 
    private NavigableMap<String, String> init() {