添加<键,值>在while循环的hashmap中但不起作用 [英] Adding <Key, Value> in hashmap by while loop but not working

查看:30
本文介绍了添加<键,值>在while循环的hashmap中但不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 CSV 文件,我想创建一个哈希图,其中唯一键应该是每行的第一个值,值应该是一个 arraylist,其中包含每个行的信息该行的第一个值.CSV 示例如下:

I have the following CSV file and I would like to create a hashmap which the unique key should be the first value of each row and value should be an arraylist containing the information of each line under the first value of this row. Example CSV as below:

1,0
1,1
1,2
1,3
1,4
2,0
2,1
2,2
4,0
10,0
10,1
10,2
10,3
10,4
10,5

所以理想情况下,我希望最终的 ArrayList 类似于:

So ideally, I would like the final ArrayList to be something like:

{1=[1,0, 1,1, 1,2, 1,3, 1,4], 2=[2,0, 2,1, 2,2], 4=[4,0], 10=[10,0, 10,1, 10,2, 10,3, 10,4, 10, 5]}

下面是我用来通过while循环解决这个问题的代码

Below is the code I used trying to solve this problem by a while loop

public static void main(String[] args) {
        int activityRecord = 1;
        String activitiesFile = "scenarios/BrusselsPopulationFromR/BrusselsActivities.csv";

        HashMap<Integer, ArrayList<String>> idAndAllActivities = new HashMap<>();
        try {
            BufferedReader activityReader = new BufferedReader(new FileReader(activitiesFile));
            String agentActivity = null;
            ArrayList activities = new ArrayList();

            while ((agentActivity = activityReader.readLine()) != null){
                String activityWithAllInfo = agentActivity;
                String[] activitySpilted = agentActivity.split(",");
                int activityAgentID = Integer.parseInt(activitySpilted[0]);

                if (activityAgentID == activityRecord){
                    activities.add(activityWithAllInfo);
                } else if (activityAgentID != activityRecord){
                    idAndAllActivities.put(activityRecord, activities);
                    activityRecord = activityAgentID;
                    activities.clear();
                    activities.add(activityWithAllInfo);
                }
            }

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

        System.out.println(idAndAllActivities);
    }

基本上,我尝试证明是否activityAgentID == activityRecord(最初为1),如果不同,我将添加<K,V>到 hashmap 并清除 activities arrayList.然而,这个程序的输出是这样的:

Basically, I try to justify whether activityAgentID == activityRecord (originally to be 1), and if it's different, I will add the <K,V> to the hashmap and clear the activities arrayList. However, the output of this program is something like:

{1=[10,0, 10,1, 10,2, 10,3, 10,4, 10,5], 2=[10,0, 10,1, 10,2, 10,3, 10,4, 10,5], 4=[10,0, 10,1, 10,2, 10,3, 10,4, 10,5]}

这不是我想要的,我试图调试这个,但我找不到原因,因为我对 Java 真的很陌生......我想知道你是否知道如何解决这个问题?任何提示将不胜感激!

This is not what I want, and I tried to debug this but I cannot find the reason for this as I am really new to Java... I am wondering if you know how to solve this? Any hints will be much appreciated!

谢谢!

推荐答案

你可以用另一种方式来解决.如果 CSV 文件的顺序更改如下,则不会中断的方法:

You could do it in another way. A way that does not break if the order of the CSV file changes as follows:

1,0
1,1
2,0
1,2
10,0
10,1
10,2
1,3
1,4
2,1
2,2
4,0
10,3
10,4
10,5

使用此 CSV 您的代码将失败,因为您依赖 CSV 文件将所有 1 活动记录放在一起.为了解决这个问题,请尝试以下操作(请注意,您不再依赖 activityRecord):

With this CSV your code will fail because you are relying on the CSV file to have all 1 activity records together. In order to cope with this, try the following (notice that you are no longer relying on activityRecord):

public static void main(String[] args) {
    String activitiesFile = "scenarios/BrusselsPopulationFromR/BrusselsActivities.csv";

    HashMap<Integer, ArrayList<String>> idAndAllActivities = new HashMap<>();
    try {
        BufferedReader activityReader = new BufferedReader(new FileReader(activitiesFile));
        String agentActivity = null;

        while ((agentActivity = activityReader.readLine()) != null){
            String activityWithAllInfo = agentActivity;
            String[] activitySpilted = agentActivity.split(",");
            int activityAgentID = Integer.parseInt(activitySpilted[0]);

            if (idAndAllActivities.containsKey(activityAgentID)){
                ArrayList existentActivities = idAndAllActivities.get(activityAgentID);
                existentActivities.add(activityWithAllInfo);
            } else {
                ArrayList activities = new ArrayList();
                activities.add(activityWithAllInfo);
                idAndAllActivities.put(activityAgentID, activities);
            }
        }

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

    System.out.println(idAndAllActivities);
}

这篇关于添加&lt;键,值&gt;在while循环的hashmap中但不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆