import java.io.*; import java.util.*; import javax.swing.JFileChooser; /** * Simple demo of Map classes to track * how many times every word in a file occurs. * @author Owen Astrachan */ public class MapDemo { public static double testMap(BufferedReader r, Map map) { try { double start = System.currentTimeMillis(); String line; while ((line = r.readLine()) != null) { StringTokenizer toks = new StringTokenizer(line); while (toks.hasMoreTokens()) { String s = toks.nextToken().toLowerCase(); Counter c = (Counter) map.get(s); if (c == null) { map.put(s,new Counter(1)); } else { c.increment(); } } } double end = System.currentTimeMillis(); return (end - start)/1000; } catch (IOException e) { System.out.println("trouble reading "+e); } return 0; } public static double testArrayList(BufferedReader r, ArrayList list) { try { double start = System.currentTimeMillis(); String line; while ((line = r.readLine()) != null) { StringTokenizer toks = new StringTokenizer(line); while (toks.hasMoreTokens()) { String s = toks.nextToken().toLowerCase(); list.add(s); } } double end = System.currentTimeMillis(); return (end - start)/1000; } catch (IOException e) { System.out.println("trouble reading "+e); } return 0; } public static double processArray(ArrayList list) { double start = System.currentTimeMillis(); int max = 0; int count = 1; int run = 1; Object maxObject = null; Object last = list.get(0); for(int k=1; k < list.size(); k++) { Object current = list.get(k); if (! current.equals(last)) { count++; if (run > max) { max = run; maxObject = last; } run = 0; last = current; } else { run++; } } double end = System.currentTimeMillis(); System.out.println("# different objects "+count); System.out.println("most frequent "+maxObject); return (end-start)/1000; } /** * Returns the maximally occurring string in map * @returns the word that occurs more frequently than any other */ public static String mostFrequent(Map map) { Iterator it = map.keySet().iterator(); int max = 0; String smax = null; while (it.hasNext()) { String s = (String) it.next(); int count = ((Counter)map.get(s)).getValue(); if (count > max) { max = count; smax = s; } } return smax; } public static void main(String[] args) { String filename = null; if (args.length > 0) { filename = args[0]; } else { JFileChooser chooser = new JFileChooser("."); int retval = chooser.showOpenDialog(null); if (retval == JFileChooser.APPROVE_OPTION) { filename = chooser.getSelectedFile().getPath(); System.out.println("testing "+filename); } else { filename = "MapDemo.java"; } } try { BufferedReader reader = new BufferedReader( new FileReader(filename)); TreeMap tmap = new TreeMap(); double treeTime = testMap(reader,tmap); reader = new BufferedReader( new FileReader(filename)); HashMap hmap = new HashMap(); double hashTime = testMap(reader,hmap); String max = mostFrequent(tmap); System.out.print("TreeMap size = "+tmap.size()); System.out.println(" in time "+treeTime); System.out.println("most frequent word is: "+max); max = mostFrequent(hmap); System.out.print("HashMap size = "+hmap.size()); System.out.println(" in time "+hashTime); System.out.println("most frequent word is: "+max); reader = new BufferedReader( new FileReader(filename)); ArrayList list = new ArrayList(); double atime = testArrayList(reader,list); double start = System.currentTimeMillis(); Collections.sort(list); double end = System.currentTimeMillis(); System.out.println("reading array time "+atime); double sortTime = (end-start)/1000; System.out.println("sorting time "+sortTime); double ptime = processArray(list); System.out.println("total time "+(atime+sortTime+ptime)); Iterator it = tmap.keySet().iterator(); while (it.hasNext()) { String s = (String) it.next(); System.out.println(tmap.get(s) + "\t" + s); } } catch (IOException e) { System.out.println("problem creating reader "+e); } System.exit(0); // need this to close JFileChooser } }