Archives: Map

Java – NavigableMap Example

[code lang=”java”]import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapDemo {

public static void main(String[] args) {

NavigableMap<String,Integer> navigableMap = new TreeMap<String, Integer>();

navigableMap.put("X", 500);
navigableMap.put("B", 600);
navigableMap.put("A", 700);
navigableMap.put("T", 800);
navigableMap.put("Y", 900);
navigableMap.put("Z", 200);

System.out.printf("Descending Set : %s%n",navigableMap.descendingKeySet());

System.out.printf("Floor Entry : %s%n",navigableMap.floorEntry("L"));

System.out.printf("First Entry : %s%n",navigableMap.firstEntry());

System.out.printf("Last Key : %s%n",navigableMap.lastKey());

System.out.printf("First Key : %s%n",navigableMap.firstKey());

System.out.printf("Original Map : %s%n",navigableMap);

System.out.printf("Reverse Map : %s%n",navigableMap.descendingMap());

}

}[/code]
Output:

Descending Set : [Z, Y, X, T, B, A]
Floor Entry : B=600
First Entry : A=700
Last Key : Z
First Key : A
Original Map : {A=700, B=600, T=800, X=500, Y=900, Z=200}
Reverse Map : {Z=200, Y=900, X=500, T=800, B=600, A=700}


Java – Convert Array to Map using Commons ArrayUtils

This example use the Apache Commons lang’s ArrayUtils class to convert a two dimensional array to a Map object.


Java – Convert Map to JSON

Output:

json = {“WHITE”:”#FFFFFF”,”BLUE”:”#0000FF”,”YELLOW”:”#FFFF00″,”GREEN”:”#008000″,”BLACK”:”#000000″,”RED”:”#FF0000″}
map.get = #FFFFFF
map.get = #0000FF
map.get = #FFFF00
map.get = #008000
map.get = #000000
map.get = #FF0000