Advertisement
cd62131

Sort Map

Jan 19th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.99 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.Map;
  3. import java.util.Map.Entry;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class SortList {
  8.   public static void main(String[] args) {
  9.     Scanner in = null;
  10.     try {
  11.       in = new Scanner(new File("List1.txt"));
  12.     }
  13.     catch (Exception ex) {
  14.       ex.printStackTrace();
  15.     }
  16.     Map<String, Integer> items = new TreeMap<String, Integer>();
  17.     while (in.hasNextLine()) {
  18.       String key = in.nextLine().trim();
  19.       if (!items.containsKey(key)) items.put(key, 0);
  20.       items.put(key, items.get(key) + 1);
  21.     }
  22.     in.close();
  23.     while (!items.isEmpty()) {
  24.       int larger = Integer.MIN_VALUE;
  25.       Entry<String, Integer> item = null;
  26.       for (Entry<String, Integer> e: items.entrySet()) {
  27.         if (larger < e.getValue()) {
  28.           larger = e.getValue();
  29.           item = e;
  30.         }
  31.       }
  32.       System.out.println(item.getKey() + item.getValue());
  33.       items.remove(item.getKey());
  34.     }
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement