Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to obtain a key ordering of a map by its values effectively or no map

807607Jan 16 2007 — edited Jan 17 2007
i know that map generally is not intended for sorting by values, but i do want to get a key ordering by its values in desc order since all data is stored into map previously

and here is the code i tried to may it work, it works but i guess the cost time will be a map.keyset.size() * map.values.size() double squared

Map a = new HashMap();
a.put("1", new BigDecimal("1.2"));
a.put("2", new BigDecimal("1.3"));
a.put("3", new BigDecimal("1.1"));
a.put("4", new BigDecimal("1.0"));
a.put("5", new BigDecimal("1.6"));
a.put("6", new BigDecimal("1.0"));
List values = new ArrayList(a.values());
Collections.sort(values);
BigDecimal bd = null;
Set entry = a.entrySet();
List rsList = new ArrayList();
for(Iterator it = values.iterator(); it.hasNext();)
{
bd = (BigDecimal) it.next();
for(Iterator ij = entry.iterator(); ij.hasNext();)
{
Map.Entry et = (Map.Entry) ij.next();
if(et.getValue().toString().equals(bd.toString()))
{
System.out.println("gotit" + et.getKey());
rsList.add(et.getKey());
ij.remove();
}
}
}

is there any better way to perform this operation or i really should not use this data structure?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 14 2007
Added on Jan 16 2007
2 comments
437 views