posted 19 years ago
Here's the code what I have now. I'll try out the LinkedHashSet as you suggested for retaining the order of the Set.
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.lang.String;
public class CompareFiles {
public static void main(String[] argv){
try {
String basefile = argv[0];
String inputfile = argv[1];
HashMap base_hash = readFile(basefile);
HashMap input_hash = readFile(inputfile);
Set entries = base_hash.entrySet();
for (Iterator it = entries.iterator(); it.hasNext(); )
{
Map.Entry entry = (Map.Entry) it.next();
Set val = (Set) entry.getValue();
// loop through info to check for desired value
val.containsAll((Set) input_hash.get(entry.getKey()));
}
System.out.println("Bash Hash : " + base_hash);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("\n" + "You must specify the base file and a file name as argument" + "\n");
System.out.println("USAGE: java CompareFiles <BaseFileName> <InputFileName>" + "\n");
}
}
public static HashMap readFile(String file_name) {
HashMap hm = new HashMap();
try {
// Reading file
BufferedReader reader = new BufferedReader(new FileReader(file_name));
List list = new ArrayList();
String line = reader.readLine();
while(line != null){
list.add(line);
line = reader.readLine();
}
reader.close();
// Setting delimiter
Pattern p = Pattern.compile(",");
//StringBuffer val = new StringBuffer();
Set value = new HashSet();
// records are in the array one line per element
// also, each was printed to stout as it was read
Iterator iterator = list.iterator();
while(iterator.hasNext()){
String str = (String) iterator.next();
// Parsing each line by delimiter
String[] result = p.split(str);
// Storing the first value from the String array as the key
String key = result[0];
// Rest of the String array will be the value
for (int i=1; i<result.length; i++)
{
//val.append(result[i] + ',');
value.add(result[i]);
}
System.out.println("Key is " + key + " Value is " + value);
hm.put(key, value);
}
System.out.println("ReadFile " + hm);
} catch(Exception ex){
System.out.println(ex);
}
return hm;
}
}
Here's the 2 input files -
input1 :
1,abc,cde,efg
2,ghi,jkl, lmn
3,nop,pqr,stw
input2 :
1,111,cde,efg
2,222,jkl, lmn
3,333,stv,lmn
Expected output:
1,abc,cde,efg,111,cde,efg
2,ghi,jkl, lmn,222,jkl, lmn
3,nop,pqr,stw,333,stv,lmn
Here's the output I am getting -
%java CompareFiles input1 input2
Key is 1 Value is [cde, abc, efg]
Key is 2 Value is [ghi, lmn, cde, abc, jkl, efg]
Key is 3 Value is [ghi, stw, nop, pqr, lmn, cde, abc, jkl, efg]
ReadFile {3=[ghi, stw, nop, pqr, lmn, cde, abc, jkl, efg], 2=[ghi, stw, nop, pqr, lmn, cde, abc, jkl, efg], 1=[ghi, stw, nop, pqr, lmn, cde, abc, jkl, efg]}
Key is 1 Value is [cde, 111, efg]
Key is 2 Value is [222, lmn, cde, 111, jkl, efg]
Key is 3 Value is [lmn, 222, 333, lmn, cde, stv, 111, jkl, efg]
ReadFile {3=[lmn, 222, 333, lmn, cde, stv, 111, jkl, efg], 2=[lmn, 222, 333, lmn, cde, stv, 111, jkl, efg], 1=[lmn, 222, 333, lmn, cde, stv, 111, jkl, efg]}
Bash Hash : {3=[ghi, stw, nop, pqr, lmn, cde, abc, jkl, efg], 2=[ghi, stw, nop, pqr, lmn, cde, abc, jkl, efg], 1=[ghi, stw, nop, pqr, lmn, cde, abc, jkl, efg]}
Thanks for all your help!