Advertisement
Guest User

Untitled

a guest
Mar 30th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. package wordcounttest;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Scanner;
  11.  
  12. /**
  13.  *
  14.  * @author Dan Princ
  15.  * @date 30.3.2013
  16.  */
  17. public class WordCountTest {
  18.    
  19.     private static final int ITERATIONS = 10000000;
  20.    
  21.     public static void main(String[] args) throws IOException {
  22.    
  23.     File file = new File("test.txt");
  24.     List<String> names = new ArrayList<>();
  25.     Scanner in = new Scanner(file);
  26.     while (in.hasNextLine()) {
  27.         names.add(in.nextLine());
  28.     }
  29.    
  30.     long start, end;
  31.    
  32.     start = System.currentTimeMillis();
  33.     for(int i = 0; i < ITERATIONS; i++) {
  34.         getCount(names);
  35.     }
  36.     end = System.currentTimeMillis();
  37.     System.out.println("time 1: " + (end - start));
  38.    
  39.     start = System.currentTimeMillis();
  40.     for(int i = 0; i < ITERATIONS; i++) {
  41.         countAlphabeticalWords(names);
  42.     }
  43.     end = System.currentTimeMillis();
  44.     System.out.println("time 2: " + (end - start));
  45.    
  46.  
  47.     }
  48.    
  49.     public static void getCount(List<String> list) {
  50.     final int[] data = new int[26];
  51.     final int a = (int) 'a';
  52.  
  53.     for(String s : list) {
  54.         data[((int) Character.toLowerCase(s.charAt(0))) - a]++;
  55.     }
  56.  
  57. //  for(int i = 0; i < 26; i++) {
  58. //      System.out.println("The number of words begining with " + (char) (a + i) + " are: " + data[i]);
  59. //  }
  60.     }
  61.    
  62.     public static void  countAlphabeticalWords(List<String> list) throws IOException {
  63.     Map<Character,Integer> counts = new HashMap<>();
  64.     //String word = "";
  65.  
  66.     for(String word : list) {
  67.       Character c = Character.toUpperCase(word.charAt(0));
  68.       if (counts.containsKey(c)) {
  69.         counts.put(c, counts.get(c) + 1);
  70.       }
  71.       else {
  72.         counts.put(c, 1);
  73.       }
  74.     }
  75.  
  76. //  for (Map.Entry<Character, Integer> entry : counts.entrySet()) {
  77. //      System.out.println("The number of words begining with " + entry.getKey() + " are: " + entry.getValue());
  78. //  }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement