HI there. My 1st post here, and already i've got a deal of questions to ask. I believe all of u know how a typical index looks like in large or reference books. For eg. : Acetone, 381 Acid rain, 95 Base-pairs, 1197 Dacron, 4, 856, 1245 Electron donors, 676 Elimination, 191, 215-217,227, 267 The index keys are sorted alphabetically and each key is followed by a list of page numbers. Now I'm in the middle of making a program that will process a raw index file and generate an index. The program will get arguments as two file names. The first argument will stand for the name of an input raw index file and the second argument for the output index file. The input raw index file will contain index entities in the following format: Dacron ; 4 Acid rain ; 95 Elimination ; 191 Elimination ; 215 Elimination ; 216 Elimination ; 217 Elimination ; 227 Elimination ; 267 Acetone ; 381 Electron donors ; 676 Dacron ; 856 Base-pairs ; 1197 Dacron ; 1245 The program will process the raw index file and write to the output file the generated index as described above. For eg : Elimination, 191, 215-217,227, 267 I'm using java.io package : Code: import java.io.*; /** * The program reads a raw index file and prints it to another file, * using a different format. */ public class IndexExample { [INDENT]final static char DELIMITER = ';';[/INDENT] [INDENT]public static void main(String[] args) {[/INDENT] [INDENT][INDENT]if (args.length != 2) {[/INDENT][/INDENT] [INDENT][INDENT][INDENT]System.out.println("Please supply two file names");[/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]return;[/INDENT][/INDENT][/INDENT] [INDENT][INDENT]}[/INDENT][/INDENT] [INDENT][INDENT]try {[/INDENT][/INDENT] [INDENT][INDENT][INDENT]BufferedReader in = new BufferedReader(new [/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]FileReader(args[0]));[/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]PrintWriter out = new PrintWriter(new FileWriter(args[1]));[/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]String line;[/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]while ((line = in.readLine()) != null) {[/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]int delimiterIndex = line.indexOf(DELIMITER);[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]if (delimiterIndex < 0) {[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT][INDENT]continue;[/INDENT][/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]}[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]String argument1 = line.substring(0,delimiterIndex-1);[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]String argument2 = line.substring(delimiterIndex+1, line.length());[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]String key = (argument1).trim();[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]Integer pageNumber = new Integer(argument2.trim());[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT][INDENT]System.out.println("keyword = " + key + " ; page no. = " + pageNumber);[/INDENT][/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]}[/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]in.close();[/INDENT][/INDENT][/INDENT] [INDENT][INDENT][INDENT]out.close();[/INDENT][/INDENT][/INDENT] [INDENT][INDENT]} catch (IOException e) {[/INDENT][/INDENT] [INDENT][INDENT]/* dont bother the exception */[/INDENT][/INDENT] [INDENT][INDENT]}[/INDENT][/INDENT] [INDENT]}[/INDENT] } Now my question is : I'd like to try using TreeMap and TreeSet objects to represent the collection of index entries and page sets respectively. Is this achievable ? I'm open to critics and suggestions. Feel free to give any input.
Hi, Do you want to populate indexes and corresponding pages to TreeMap ?. Then the answer could be, 1) create a simple map inserting all indexes and corresponding pages to the map. and use constructor of TreeMap to create a new TreeMap using map as parameter to it. eg, Map map = new HashMap(); loop: map.put(index,page); TreeMap treeMap = new TreeMap(map); It will simply sort your pages according to indexes in ascending order. please let me know i have answered your question ?