Here is a Java Program to Demonstrate TreeMap
Output of Above Java Program
Jane Baker: 1378.0
John Doe: 3434.34
Ralph Smith: -19.08
Tod Hall: 99.22
Tom Smith: 123.22
John Doe's new balance: 4434.34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.util.*; class TreeMapDemo { public static void main(String args[]) { // Create a tree map TreeMap tm = new TreeMap(); // Put elements to the map tm.put( "John Doe" , new Double( 3434.34 )); tm.put( "Tom Smith" , new Double( 123.22 )); tm.put( "Jane Baker" , new Double( 1378.00 )); tm.put( "Tod Hall" , new Double( 99.22 )); tm.put( "Ralph Smith" , new Double(- 19.08 )); // Get a set of the entries Set set = tm.entrySet(); // Get an iterator Iterator i = set.iterator(); // Display elements while (i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": " ); System.out.println(me.getValue()); } System.out.println(); // Deposit 1000 into John Doe's account double balance = ((Double)tm.get( "John Doe" )).doubleValue(); tm.put( "John Doe" , new Double(balance + 1000 )); System.out.println( "John Doe's new balance: " + tm.get( "John Doe" )); } } |
Output of Above Java Program
Jane Baker: 1378.0
John Doe: 3434.34
Ralph Smith: -19.08
Tod Hall: 99.22
Tom Smith: 123.22
John Doe's new balance: 4434.34