Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Map.Entry;
  8. import java.util.TreeMap;
  9.  
  10. public class Str {
  11.  
  12. public static void main(String[] args) {
  13. HashMap<String,Integer> m = new HashMap<String,Integer>(); Integer t;
  14. for(int i=0;i<args.length;i++){
  15. if(m.containsKey(args[i])) {
  16. t=m.remove(args[i]);
  17. ++t;
  18. m.put(args[i], t);
  19. }else {
  20. m.put(args[i], 1);
  21. }
  22. }
  23. List<Map.Entry<String,Integer>> s = new ArrayList(m.entrySet());
  24. Comparator<Entry<String,Integer>> c = new MyComparator<Entry<String,Integer>>();
  25. Collections.sort(s, c);
  26. displayStrings(s);
  27. }
  28.  
  29. private static void displayStrings(List<Entry<String, Integer>> s) {
  30. for(Entry<String, Integer> str:s){
  31. System.out.println(str.getKey()+" "+str.getValue());
  32. }
  33. }
  34.  
  35. }
  36.  
  37.  
  38. import java.util.Comparator;
  39.  
  40. public class MyComparator<T> implements Comparator<Entry<String, Integer>> {
  41.  
  42. @Override
  43. public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
  44. // TODO Auto-generated method stub
  45. return o2.getValue() - o1.getValue();
  46. }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement