Click to See Complete Forum and Search --> : TreeMaps
Rabbit3cat
02-16-2006, 07:43 AM
Im trying to return just the top 10 values from a treeMap, does anybody know how to do this, or where i am going wrong. Here is what i have been doing
Set set = treemap.keySet();
Iterator it = set.iterator();
for(int i = 0; i < 10; i++) {
while(it.hasNext()) {
Object o = it.next();
System.out.println( o + " " + treemap.get(o));
}
}
}
Thanks
Khalid Ali
02-16-2006, 02:22 PM
what is the result u get? because to get top ten values you will need to set ur own criteria to make sure that which values are top ten and which is top value...
Rabbit3cat
02-17-2006, 07:48 AM
Thanks, currently that code is returning ever value in the tree map. The loop i have entered is having no visible effect. The treemap contains doubles as keys, and strings as values like below:
{21.518988=bye, 18.26923=night, 15.306123=tree, 13.736183=hello....}
What i want to do is return the 10 key-value pairs with the highest double as keys so in this case if i was to return the top three they would be:
21.518988=bye, 18.26923=night, 15.306123=tree
Khalid Ali
02-17-2006, 11:29 AM
for this purpose you will have to process each double value and get the largest number out of them and then put them in anohter list(may be?) and then return that...