Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Map;
- import java.util.HashMap;
- /**
- * java.util.Map driver
- * Reads items to put in a Map from standard input,
- * then reports the elapsed time and unique word count.
- * @author jdalbey
- */
- public class Mapdriver
- {
- public static void main (String args[]) throws java.io.FileNotFoundException
- {
- Map<String,Integer> ht = new HashMap<String,Integer>();
- Scanner in = new Scanner(System.in);
- System.out.println("Enter a list of words (one per line), terminated by Ctrl-D");
- long start = System.currentTimeMillis();
- int wordCount = 0;
- // Put all the words in the map
- while (in.hasNext())
- {
- String word = in.next();
- // If word doesn't exist in map, add it
- if (!ht.containsKey(word))
- {
- ht.put(word,1);
- wordCount++;
- }
- }
- // compute elapsed time
- long end = System.currentTimeMillis();
- float secs = (end - start) / 1000f;
- // show results
- System.out.print("Took " + secs + " secs. " );
- System.out.println("wordCount: " + wordCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment