Java.util.TreeMap--Java Collections
Java.util.TreeMap
//Java Collections -TreeMap
import
java.security.KeyStore.Entry;
import
java.util.Iterator;
import
java.util.Map;
import
java.util.TreeMap;
// Darur VenuGopal
public
class
TreeMapDemo {
/**
*
Build
a
TREE
MAP
*
compatible
to
java1.5,
but
not
suggested
to
use
since
this
is
not
using
java
generics
*
*
Put
a
String
key
and
String
value
*/
public
TreeMap buildTreeMap( ){
TreeMap treeMap =
new
TreeMap<String, String>();
treeMap.put(
"URL"
, "HTTP://WWW.IFORERUNNER.COM"
);
treeMap.put(
"URL1"
, "HTTP://WWW.IFORERUNNER8.COM"
);
treeMap.put(
"URL2"
,
"HTTP://WWW.IFORERUNNER7.COM"
);
treeMap.put(
"URL3"
,
"HTTP://WWW.IFORERUNNER6.COM"
);
treeMap.put(
"URL4"
,
"HTTP://WWW.IFORERUNNER5.COM"
);
treeMap.put(
"URLfive"
,
"HTTP://WWW.IFORERUNNER4.COM"
);
treeMap.put(
"URLFive"
,
"HTTP://WWW.IFORERUNNER3.COM"
);
treeMap.put(
"URLSix"
,
"HTTP://WWW.IFORERUNNER2.COM"
);
treeMap.put(
"URLsix"
,
"HTTP://WWW.IFORERUNNER1.COM"
);
return
treeMap;
}
/**
*
Get
the
MAP
key
Values,
*
Get
the
value
assigned
to
a
key
*
print
the
key
and
value
*
@param
treeMap
*/
public
void
printTreeMap( TreeMap treeMap ){
Iterator
iterator = treeMap.entrySet().iterator();
while (
iterator.hasNext() ){
Map.Entry
keyEntry = (Map.Entry ) iterator.next();
String key = (
String )keyEntry.getKey();
System. out.print(
" KEY : "
+ key);
String value =
(String) treeMap.get(key);
System. out.println(
" \t\t\tThe
Value for the key: "
+ value );
}
}
public
static
void
main( String [] args ){
TreeMapDemo
treeMapDemo =
new
TreeMapDemo();
TreeMap
treeMap = treeMapDemo.buildTreeMap();
treeMapDemo.printTreeMap(treeMap);
}
}
Output :
KEY : URL The Value for the key:
HTTP://WWW.IFORERUNNER.COM
KEY : URL1 The Value for the key:
HTTP://WWW.IFORERUNNER8.COM
KEY : URL2 The Value for the key:
HTTP://WWW.IFORERUNNER7.COM
KEY : URL3 The Value for the key:
HTTP://WWW.IFORERUNNER6.COM
KEY : URL4 The Value for the key:
HTTP://WWW.IFORERUNNER5.COM
KEY : URLFive The Value for the key:
HTTP://WWW.IFORERUNNER3.COM
KEY : URLSix The Value for the key:
HTTP://WWW.IFORERUNNER2.COM
KEY : URLfive The Value for the key:
HTTP://WWW.IFORERUNNER4.COM
KEY : URLsix The Value for the key:
HTTP://WWW.IFORERUNNER1.COM
Please send comments to vgdarur.javafive@blogger.com
|