/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hashtableexample; import java.util.*; /** * * @author kvaradar */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String [] arr = {"long", "long", "ago", "in", "a", "galaxy", "far", "far", "away"}; HashMap h = new HashMap(); MyWord w; for (int i = 0; i < arr.length; i++ ) { w = h.get(arr[i]); if (w == null) h.put(arr[i], new MyWord(arr[i])); else w.incrementFrequency(); } Collection c = h.values(); for (MyWord v: c) System.out.println(v.getElement() + ": " + v.getFrequency()); } }