比较两个ArrayList并根据条件打印合并的arraylist的数据

时间:2018-09-01 05:06:52

标签: java arraylist

我使用的两个文件是 1. Details.txt

Code|Account No.|City
100051001|999001|Delhi
200051003|999002|Kanpur
180051012|999003|Jammu

2.Sal.txt

Code|Amount 
100051001|100
200051001|200
180051012|123

输出应为

100051001   999001  Amount  100  INR  Delhi
180051012   999003  Amount  123  INR  Jammu

我尝试过的代码如下:-

//Code to get the output
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class compareArrayList {

        public static void main(String[] args) throws FileNotFoundException {

            String credit_file_name ="C:/transfer/Sal.txt";
            String file_branches ="C:/transfer/Details.txt";

            BufferedReader reader0;
            BufferedReader reader1;
            ArrayList<String> branch_list = new ArrayList<String>();
            ArrayList<String> credit_file = new ArrayList<String>();

                try {                   
                    reader0 = new BufferedReader(new FileReader(file_branches));
                    reader1 = new BufferedReader(new FileReader(credit_file_name));
                    String data ="";
                    String acct_num = "";
                    String city ="";
                    String amount1 ="";
                    String line0 = reader0.readLine();
                    String line1 = reader1.readLine();

                        while (line0 != null) {
                            branch_list.add(line0); 
                            line0 = reader0.readLine();
                            } 
                        Collections.sort(branch_list);
                        reader0.close();

                        while (line1 != null) {
                            credit_file.add(line1);     
                            line1 = reader1.readLine();
                                } 
                        Collections.sort(credit_file);
                        reader1.close();


                    for ( int i = 0; i < branch_list.size(); i++){

                        for (int j = 0; j < credit_file.size(); j++) {

                            int firstIndex0 = branch_list.get(i).indexOf('|',1);
                            int secondIndex0 = branch_list.get(i).indexOf('|', firstIndex0 +1);
                            int firstIndex1 = credit_file.get(j).indexOf('|',1);

                            acct_num = branch_list.get(i).substring(firstIndex0+1, secondIndex0);
                            city = branch_list.get(i).substring(secondIndex0+1, branch_list.get(i).length());
                            amount1 = credit_file.get(j).substring(firstIndex1+1, credit_file.get(j).length());

                                    if ( branch_list.get(i).substring(0, 9).equals(credit_file.get(j).substring(0, 9)))                                                                                                         
                                        data = branch_list.get(i).substring(0, 9) + "   " + acct_num + "  Amount  " + amount1 + "  INR  "   + city;                                                                     
                                }                       
                             System.out.println(data);
                        }                                                                           

                    }
                    catch (IOException e) {
                                e.printStackTrace();
                                }               
        }       
}

我得到的输出如下:-

100051001   999001  Amount  100  INR  Delhi
180051012   999003  Amount  123  INR  Jammu
180051012   999003  Amount  123  INR  Jammu

此记录重复。

2 个答案:

答案 0 :(得分:0)

您可以使用map消除重复值。我假设acc_no是唯一的,所以修改后的代码是:-

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class compareArrayList {

    public static void main(String[] args) throws FileNotFoundException {

        String credit_file_name = "C:/transfer/Sal.txt";
        String file_branches = "C:/transfer/Details.txt";

        BufferedReader reader0;
        BufferedReader reader1;
        ArrayList<String> branch_list = new ArrayList<String>();
        ArrayList<String> credit_file = new ArrayList<String>();

        try {
            reader0 = new BufferedReader(new FileReader(file_branches));
            reader1 = new BufferedReader(new FileReader(credit_file_name));
            String data = "";
            Long acct_num = null;
            String city = "";
            String amount1 = "";
            String line0 = reader0.readLine();
            String line1 = reader1.readLine();

            Map<Long, String> maps = new HashMap<>();
            while (line0 != null) {
                branch_list.add(line0);
                line0 = reader0.readLine();
            }
            Collections.sort(branch_list);
            reader0.close();

            while (line1 != null) {
                credit_file.add(line1);
                line1 = reader1.readLine();
            }
            Collections.sort(credit_file);
            reader1.close();

            for (int i = 0; i < branch_list.size(); i++) {

                for (int j = 0; j < credit_file.size(); j++) {

                    int firstIndex0 = branch_list.get(i).indexOf('|', 1);
                    int secondIndex0 = branch_list.get(i).indexOf('|', firstIndex0 + 1);
                    int firstIndex1 = credit_file.get(j).indexOf('|', 1);

                    acct_num = new Long(branch_list.get(i).substring(firstIndex0 + 1, secondIndex0));
                    city = branch_list.get(i).substring(secondIndex0 + 1, branch_list.get(i).length());
                    amount1 = credit_file.get(j).substring(firstIndex1 + 1, credit_file.get(j).length());

                    if (branch_list.get(i).substring(0, 9).equals(credit_file.get(j).substring(0, 9)))
                        data = branch_list.get(i).substring(0, 9) + "   " + acct_num + "  Amount  " + amount1 + "  INR  " + city;

                    maps.put(acct_num, data);
                }
                //System.out.println(data);
            }

            for (String value : maps.values())
                System.out.println(value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

这是我将用来解决此类问题的方法。

  1. 我会使用一个可以读取CSV文件的库,例如supercsvopencsv读取文件并将其转换为Java对象。
  2. 创建一个可以表示您的数据的Java类
  3. 创建地图并使用acct_num作为键添加第一个文件中的项目
  4. 遍历第二个文件中的项目并更新地图