import java.util.*; import java.io.*; class Concordance { private Map wordMap = new TreeMap(); public Concordance(String fileName) { try { BufferedReader br = new BufferedReader(new FileReader(fileName)); String oneLine; // Read the words; add them to wordMap for( int lineNum = 1; ( oneLine = br.readLine() ) != null; lineNum++ ) { StringTokenizer st = new StringTokenizer( oneLine ); while( st.hasMoreTokens() ) { String word = st.nextToken(); List lines = (List) wordMap.get( word ); if( lines == null ) { lines = new LinkedList(); wordMap.put( word, lines ); } lines.add( new Integer( lineNum ) ); } } } catch( IOException e ) { e.printStackTrace(); } } public void print() { Iterator mapIter = wordMap.entrySet().iterator(); while (mapIter.hasNext() ) { Map.Entry entry = (Map.Entry)mapIter.next(); // Print the word System.out.println( entry.getKey() + ":" ); // Now print the line numbers Iterator itr = ( (List)(entry.getValue()) ).iterator(); System.out.print( "\t" + itr.next() ); while( itr.hasNext() ) System.out.print( ", " + itr.next() ); System.out.println(); } } public static void main( String [] args ) { if (args.length == 0) { System.err.println("file not specified."); System.exit(1); } Concordance conc = new Concordance(args[0]); conc.print(); } }