Advertisement
ffpaladin

Word up... Just some more practice.

Mar 14th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.Map.Entry;
  4.  
  5. public class WordUp {
  6.    
  7.     public static void main(String[] args){
  8.        
  9.         //readFile("src/john.txt");
  10.        
  11.         HashMap<String,Integer> hist = new HashMap<String,Integer> ();
  12.        
  13.         readCount("src/john.txt",hist);
  14.        
  15.         Map<String, Integer> sorted = sortByValues(hist);
  16.  
  17.         System.out.println("Sorted Map in Java by key: " + sorted);
  18.  
  19.        
  20.         // -----------------------
  21.  
  22.         try{
  23.             BufferedWriter out = new BufferedWriter(new FileWriter("src/output.txt"));
  24.        
  25.             for (Map.Entry<String, Integer> entry : sorted.entrySet()) {
  26.                 String key = entry.getKey();
  27.                 Integer value = entry.getValue();
  28.                 out.write(key + " = "  + value + "\n");
  29.             }
  30.            
  31.             out.close();   
  32.         }
  33.         catch(Exception e){
  34.             e.printStackTrace();
  35.         }    
  36. }
  37.  
  38.            
  39.     public static void readFile (String filename){
  40.         try{
  41.             BufferedReader in = new BufferedReader(new FileReader(filename));
  42.             for (String s = in.readLine(); s != null; s = in.readLine()){
  43.                 System.out.println(s);
  44.             }
  45.            
  46.             in.close();
  47.         }
  48.         catch(Exception e){
  49.             e.printStackTrace();
  50.         }
  51.        
  52.     }
  53.    
  54.     public static void readCount (String filename, HashMap<String,Integer> h){
  55.         try{
  56.             BufferedReader in = new BufferedReader(new FileReader(filename));
  57.            
  58.             for (String s = in.readLine(); s != null; s = in.readLine()){
  59.            
  60.                 String[] words = s.toLowerCase().split("[\\p{Punct}\\s\\d]+");
  61.                
  62.                 for(String k:words){
  63.                     if (h.containsKey(k))
  64.                         h.put(k, h.get(k) + 1);
  65.                     else
  66.                         h.put(k, 1);
  67.  
  68.                 }
  69.                
  70.             }
  71.            
  72.             in.close();
  73.         }
  74.         catch(Exception e){
  75.             e.printStackTrace();
  76.         }
  77.        
  78.     }
  79.    
  80.     public static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map){
  81.         List<K> keys = new LinkedList<K>(map.keySet());
  82.         Collections.sort(keys);
  83.      
  84.         //LinkedHashMap will keep the keys in the order they are inserted
  85.         //which is currently sorted on natural ordering
  86.         Map<K,V> sortedMap = new LinkedHashMap<K,V>();
  87.         for(K key: keys){
  88.             sortedMap.put(key, map.get(key));
  89.         }
  90.      
  91.         return sortedMap;
  92.        
  93.         //http://javarevisited.blogspot.com/2012/12/how-to-sort-hashmap-java-by-key-and-value.html
  94.     }
  95.    
  96.     public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
  97.         List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());
  98.      
  99.         Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
  100.  
  101.             @Override
  102.             public int compare(Entry<K, V> o1, Entry<K, V> o2) {
  103.                 return o2.getValue().compareTo(o1.getValue());
  104.             }
  105.         });
  106.      
  107.         //LinkedHashMap will keep the keys in the order they are inserted
  108.         //which is currently sorted on natural ordering
  109.         Map<K,V> sortedMap = new LinkedHashMap<K,V>();
  110.      
  111.         for(Map.Entry<K,V> entry: entries){
  112.             sortedMap.put(entry.getKey(), entry.getValue());
  113.         }
  114.      
  115.         return sortedMap;
  116.        
  117.         //http://javarevisited.blogspot.com/2012/12/how-to-sort-hashmap-java-by-key-and-value.html
  118.  
  119.     }
  120.    
  121.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement