java - 如何简化我的java代码,它包含更多的ArrayList来存储值

标签 java

我正在对某些模块进行 Junit 测试。我正在从 csv 文件读取输入数据和预期输出,然后将每一列存储在一个 arrayList 中,然后将所有列 arrayList 放入 Map 中。我的问题是,如果一两列意味着可以很好地看到代码,但如果 csv 文件中有多于一列则意味着看到代码看起来很奇怪。如果可能的话,我想将这个 arrayList 简化为其他一些 java dataStructure。

    //Loading Input Data File for Default Sorting
    static String attemptsString = "colAttempts";
    static String areacodeString = "colAreaCodes";
    static String startTimeString = "colStartIme";
    static String timezoneString = "colTimeZone";
    static String firstNameString = "colFirstName";
    static String expAttemptsOrderStr = "colExpAttempts";
    static String expAreaCodesOrderStr = "colExpAreaCodes";
    static String expStartTimeOrderStr = "colExpStartTimes";
    static String expTimeZoneOrderStr = "colExpTimeZone";
    static String expFirstNameOrderStr = "colExpFirstNames";
    static List<String> CN_inputAttempts = new ArrayList<String>();
    static List<String> CN_inputAreaCodes = new ArrayList<String>();
    static List<String> CN_inputStartTime = new ArrayList<String>();
    static List<String> CN_inputTimeZone = new ArrayList<String>();
    static List<String> CN_inputFirstNames = new ArrayList<String>();
    static List<String> CN_ExpOrderAttempts = new ArrayList<String>();
    static List<String> CN_ExpOrderAreaCodes = new ArrayList<String>();
    static List<String> CN_ExpOrderStartTime = new ArrayList<String>();
    static List<String> CN_ExpOrderTimeZone = new ArrayList<String>();
    static List<String> CN_ExpOrderFirstName = new ArrayList<String>();
public static HashMap<String, List<String>> loadforConatactNameWithAreaCodes(String ContactNamesFileName) throws FileNotFoundException{

    HashMap<String,List<String>> contactNamewithAreaCodesMap = new HashMap<>();

    InputStream inputStream = new FileInputStream(ContactNamesFileName);
    try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))){
            String[] dataArray = null;
            String line = "";
            int index = 0;
            /*
             * CSV File indexes values initialization
             */
            int ATTEMPTS = 0;
            int AREA_CODES = 1;
            int START_TIME = 2;
            int TIME_ZONE = 3;
            int FIRST_NAME = 4;
            int EXP_ORDER_ATTEMPTS = 5;
            int EXP_ORDER_AREACODES = 6;
            int EXP_ORDER_STARTTIME = 7;
            int EXP_ORDER_TIMEZONE = 8;
            int EXP_ORDER_FIRST_NAME = 9;

            while ((line = br.readLine()) != null) {
                index++;
                dataArray = line.split(csvSplitBy);
                if (index == 1) {
                    continue;
                }
                if(line.contains("#")) {
                    System.out.println("Skipping commented Row in attempts Input Data Source at line "+index+" in "+ContactNamesFileName+" file");
                }else {
                    if((dataArray[ATTEMPTS].length()>0) && (dataArray[AREA_CODES].length()>0)  && (dataArray[START_TIME].length()>0) && (dataArray[TIME_ZONE].length()>0) && (dataArray[FIRST_NAME].length()>0) && (dataArray[EXP_ORDER_ATTEMPTS].length()>0)
                            && (dataArray[EXP_ORDER_AREACODES].length()>0) && (dataArray[EXP_ORDER_STARTTIME].length()>0)  && (dataArray[EXP_ORDER_TIMEZONE].length()>0) && (dataArray[EXP_ORDER_FIRST_NAME].length()>0)) {
                        CN_inputAttempts.add(dataArray[ATTEMPTS]);
                        CN_inputAreaCodes.add(dataArray[AREA_CODES]);
                        CN_inputStartTime.add(dataArray[START_TIME]);
                        CN_inputTimeZone.add(dataArray[TIME_ZONE]);
                        CN_inputFirstNames.add(dataArray[FIRST_NAME]);
                        CN_ExpOrderAttempts.add(dataArray[EXP_ORDER_ATTEMPTS]);
                        CN_ExpOrderAreaCodes.add(dataArray[EXP_ORDER_AREACODES]);
                        CN_ExpOrderStartTime.add(dataArray[EXP_ORDER_STARTTIME]);
                        CN_ExpOrderTimeZone.add(dataArray[EXP_ORDER_TIMEZONE]);
                        CN_ExpOrderFirstName.add(dataArray[EXP_ORDER_FIRST_NAME]);

                        contactNamewithAreaCodesMap.put(attemptsString,CN_inputAttempts);
                        contactNamewithAreaCodesMap.put(areacodeString,CN_inputAreaCodes);
                        contactNamewithAreaCodesMap.put(startTimeString,CN_inputStartTime);
                        contactNamewithAreaCodesMap.put(timezoneString,CN_inputTimeZone);
                        contactNamewithAreaCodesMap.put(firstNameString,CN_inputFirstNames);
                        contactNamewithAreaCodesMap.put(expAttemptsOrderStr,CN_ExpOrderAttempts);
                        contactNamewithAreaCodesMap.put(expAreaCodesOrderStr,CN_ExpOrderAreaCodes);
                        contactNamewithAreaCodesMap.put(expStartTimeOrderStr,CN_ExpOrderStartTime);
                        contactNamewithAreaCodesMap.put(expTimeZoneOrderStr,CN_ExpOrderTimeZone);
                        contactNamewithAreaCodesMap.put(expFirstNameOrderStr,CN_ExpOrderFirstName);

                    }else {
                        System.out.println("Object not created for data at line "+index+" in "+ContactNamesFileName+" file");
                    }
                }
         }              
    } catch (FileNotFoundException e) {
            e.printStackTrace();
    } catch (IOException e1) {
            e1.printStackTrace();
    }
    return contactNamewithAreaCodesMap;

}

最佳答案

Tyr 使用 List 创建一个类保存数据或创建列表列表 List<List<String>>

关于java - 如何简化我的java代码,它包含更多的ArrayList来存储值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55807214/

相关文章:

java - 具有优先级的 sortOn 函数

Java URLEncoding/Decoding 带破折号的 URL 空格

java - getResourceAsStream() 返回 null

java - Maven POM 中的 ejb-client 类型是什么?

java - 在桌面应用程序中保存用户名和密码

java - Spring Data Rest - 存储库继承创建奇怪的搜索端点

java - 配置 ReSTLet 以在 OSGi 中使用 Jetty 连接器(不是简单连接器)

java - ListView 适配器返回错误的项目

java - 如何在 Felix 请求过滤器中获取当前 Jackrabbit 用户?

java - Android - 表面 View 。在不清除屏幕的情况下更新。 (注意 : I'm a Noob)