import
java.util.*;
public
class
Tree_Map_Demo {
public
static
<K, V>
int
findPosition(K N, TreeMap<K, V> tree_map)
{
int
pos = -
1
;
if
(tree_map.containsKey(N)) {
pos = tree_map.headMap(N).size();
}
return
pos;
}
public
static
<K, V>
void
printResult(K N, TreeMap<K, V> tree_map)
{
int
pos = findPosition(N, tree_map);
if
(pos >=
0
) {
System.out.println(N +
" found at "
+
"position = "
+ pos);
}
else
{
System.out.println(N +
" not found. "
+
"Hence position = "
+ pos);
}
}
public
static
void
main(String[] args)
{
TreeMap<Integer, String> tree_map
=
new
TreeMap<Integer, String>();
tree_map.put(
10
,
"Geeks"
);
tree_map.put(
15
,
"4"
);
tree_map.put(
20
,
"Geeks"
);
tree_map.put(
25
,
"Welcomes"
);
tree_map.put(
30
,
"You"
);
System.out.println(
"TreeMap: "
+ tree_map);
int
N1 =
20
, N2 =
5
;
printResult(N1, tree_map);
printResult(N2, tree_map);
}
}