Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wordcounttest;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- /**
- *
- * @author Dan Princ
- * @date 30.3.2013
- */
- public class WordCountTest {
- private static final int ITERATIONS = 10000000;
- public static void main(String[] args) throws IOException {
- File file = new File("test.txt");
- List<String> names = new ArrayList<>();
- Scanner in = new Scanner(file);
- while (in.hasNextLine()) {
- names.add(in.nextLine());
- }
- long start, end;
- start = System.currentTimeMillis();
- for(int i = 0; i < ITERATIONS; i++) {
- getCount(names);
- }
- end = System.currentTimeMillis();
- System.out.println("time 1: " + (end - start));
- start = System.currentTimeMillis();
- for(int i = 0; i < ITERATIONS; i++) {
- countAlphabeticalWords(names);
- }
- end = System.currentTimeMillis();
- System.out.println("time 2: " + (end - start));
- }
- public static void getCount(List<String> list) {
- final int[] data = new int[26];
- final int a = (int) 'a';
- for(String s : list) {
- data[((int) Character.toLowerCase(s.charAt(0))) - a]++;
- }
- // for(int i = 0; i < 26; i++) {
- // System.out.println("The number of words begining with " + (char) (a + i) + " are: " + data[i]);
- // }
- }
- public static void countAlphabeticalWords(List<String> list) throws IOException {
- Map<Character,Integer> counts = new HashMap<>();
- //String word = "";
- for(String word : list) {
- Character c = Character.toUpperCase(word.charAt(0));
- if (counts.containsKey(c)) {
- counts.put(c, counts.get(c) + 1);
- }
- else {
- counts.put(c, 1);
- }
- }
- // for (Map.Entry<Character, Integer> entry : counts.entrySet()) {
- // System.out.println("The number of words begining with " + entry.getKey() + " are: " + entry.getValue());
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement