对map 的value进行排序,且value是float型。注意map是无序的,LinkedHashMap是有序的
private static LinkedHashMap sortTF(HashMap tfidf) {
// TODO Auto-generated method stub
//map存入list中
List> list_data = new LinkedList>(tfidf.entrySet());
//注意map是无序的,要保证顺序需要LinkedHashMap
LinkedHashMap map1 = new LinkedHashMap();
Collections.sort(list_data,new Comparator>()
{
@Override
public int compare(Map.Entry o1,
Map.Entry o2) {
// TODO Auto-generated method stub
return (int) ((o2.getValue()-o1.getValue())*1000000.0);
}
}
);
for (Iterator> it = list_data.iterator(); it.hasNext();) {
Map.Entry entry1 = it.next();
map1.put(entry1.getKey(), entry1.getValue());
//System.out.println("key:"+entry1.getKey()+"value:"+entry1.getValue());
}
return map1;
}