+ 5

How to remake this program using HashMap, you need to read two tables from two different Excel files and output the sum?

import java.io.*; import java.util.ArrayList; /* file1 - file2 = file3*/ public class Main { public static void main(String args[]) throws FileNotFoundException, IOException { String path="D:\\Тест\\"; String file1="Test1.csv"; String file2="Test2.xlsx"; String file3="p3lang.xlsx"; ArrayList<String> al1=new ArrayList(); ArrayList<String> al2=new ArrayList(); //ArrayList al3=new ArrayList(); BufferedReader CSVFile1 = new BufferedReader(new FileReader(path+file1)); String dataRow1 = CSVFile1.readLine(); while (dataRow1 != null) { String[] dataArray1 = dataRow1.split(","); for (String item1:dataArray1) { al1.add(item1); } dataRow1 = CSVFile1.readLine(); // Read next line of data. } CSVFile1.close(); BufferedReader CSVFile2 = new BufferedReader(new FileReader(path+file2)); String dataRow2 = CSVFile2.readLine(); while (dataRow2 != null) { String[] dataArray2 = dataRow2.split(","); for (String item2:dataArray2) { al2.add(item2); } dataRow2 = CSVFile2.readLine(); // Read next line of data. } CSVFile2.close(); for(String bs:al2) { al1.remove(bs); } int size=al1.size(); System.out.println(size); try { FileWriter writer=new FileWriter(path+file3); while(size!=0) { size--; writer.append(""+al1.get(size)); writer.append('\n'); } writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } }} /* How to remake this program using HashMap, you need to read two tables from two Excel files and output the sum? Thank you.*/

7th Dec 2018, 1:16 PM
drozd
drozd - avatar
2 Réponses
+ 2
I'm not sure HashMap could get the same results. If I'm reading the code right, you get all values in first file that are not duplicated in the second including any duplicates. By default, the hash would get one copy of all values in both files.
8th Dec 2018, 10:35 PM
John Wells
John Wells - avatar
+ 2
Thank you, John.
11th Dec 2018, 6:51 PM
drozd
drozd - avatar