简体   繁体   中英

I have two CSV files with 3 columns, I have to read this and compare this using hashmap()

I have two csv files having 3 columns. I have to read this and compare this using hashmap() (multiple hashmaps).

Columns:

Name,Value, Address 
aaa,1,sdasdasd
bbb,2,sadasdasd
ccc,3,dsadasds

Code:

public class CompareFile {

    public static void main(String args[]) throws IOException {
        //HashMap<String, String> File1 = new HashMap<String, String>();
        String filepath1="D:\\XYZ\\File1.csv";
        String filepath2="D:\\XYZ\\File2.csv";
        HashMap<String, HashMap<String, String>> file1= new HashMap<String, HashMap<String, String>>();
        HashMap<String, HashMap<String, String>> file2= new HashMap<String, HashMap<String, String>>();
        CompareFile compareFile = new CompareFile();
        file1=compareFile.readFileContents(filepath1);
        file2=compareFile.readFileContents(filepath2);
        //compareFile.CompareTwo(file1,file2);
    }//

    public HashMap<String, HashMap<String, String>> readFileContents(String file) throws IOException {

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line=null;
        String[] str=null;
        HashMap<String, HashMap<String, String>> mapHashMap = new HashMap<String, HashMap<String, String>>();
        while((line=bufferedReader.readLine())!=null)
        {
            str = line.split(",");
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put(str[1], str[2]);
            mapHashMap.put(str[0], hashMap);

        }
        for (Map.Entry<String, HashMap<String, String>> entry : mapHashMap.entrySet())
        {
           System.out.println(entry.getKey() + " " + entry.getValue());
        }return mapHashMap;
    }

    public void CompareTwo(HashMap<String, HashMap<String, String>> file1,HashMap<String, HashMap<String, String>> file2)throws IOException {
    {
        for(Map.Entry<String, HashMap<String, String>> entry1:file1.entrySet())
        {
            String key1=entry1.getKey();
            HashMap<String, String> value1=entry1.getValue();
            for(Map.Entry<String, HashMap<String, String>> entry2:file2.entrySet())
            {
                 String key2=entry2.getKey();
                 HashMap<String, String> value2=entry2.getValue();

                 if(key1==key2)
                 {

                     System.out.println(key2 + "\t" + value1 + "\t" + value2);
                 }


             }//

         }//
     }//
 }//

I think you should think about how to design your solution. Start by create some classes to represents what is a Csv file, what is each row in your csv file, and so on ...

For example create a java class named "CsvFile", which will contains some attributes that represent datas in your files, and these attributes could be some new java class to ("CsvRow" for example) ...

For reading your file, Use InputStreamReader java class to read and load your file.

File file = new File("C:/ ... ");
InputStream stream = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(stream, "ISO-8859-1");

Then you can loop over :

reader.read(); 

Like this :

String contentFile;
char[] buffer = new char[10000];
int n;
while ((n = reader.read(buffer)) > 0) {
    contentFile.append(buffer, 0, n);
}
reader.close();

This is just how to read your file and get the content. But you still have to load this file content into your own classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM