dcsimg
hash map sorting help
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   A_KATWA
Posted On:   Wednesday, November 20, 2002 07:35 AM

hi there i have looked at some code for using hashmaps in java and adapted it for my own needs what i need to do is to sort out the largest frequency words that appear for example the first five largest words.. ie house = 5 girl = 4 friend =3 mince =2 mate = 1 rather than just sorted alphabetically... and also what would be nice is to group the words into a new string ie "house girl friend mince mate" any help gratefully appreciated ..... import java.util.*; public class MapExample { public static void main(String args[]) { Map map = new HashMap(); Integer ONE = new Integer(1); for (i   More>>

			
hi there i have looked at some code for using hashmaps in
java and adapted it for my own needs what i need to do is
to sort out the largest frequency words that appear for
example the first five largest words.. ie
house = 5
girl = 4
friend =3
mince =2
mate = 1

rather than just sorted alphabetically... and also
what would be nice is to group the words into a new
string ie "house girl friend mince mate"

any help gratefully appreciated .....

import java.util.*;

public class MapExample {
public static void main(String args[]) {
Map map = new HashMap();
Integer ONE = new Integer(1);
for (int i=0, n=args.length; i
String key = args[i];
Integer frequency = (Integer)map.get(key);
if (frequency == null) {
frequency = ONE;
} else {
int value = frequency.intValue();
frequency = new Integer(value + 1);
}
map.put(key, frequency);
}
System.out.println(map);
Map sortedMap = new TreeMap(map);
System.out.println(sortedMap);
}
}
   <<Less
About | Sitemap | Contact