在java中使用HashMap时重复的值

时间:2015-12-01 23:00:27

标签: java arraylist hashmap

使用地图时遇到问题

我需要做的是使用hashmap构建这个矩阵

   A B C D  
 A o di o o   
 B o di o o  
 C o di o di  
 D di o o di 

o和di表示活动{A,B,C,D}之间的关系(值) 对于每个关系,我需要附加其时间戳 所以,我创建了一个这样的地图

Map<String, Map<String, ArrayList<Object>>> map = new HashMap<String,Map<String, ArrayList<Object>>>();

由于此地图的工作原理如下

A --> B --> {o,specific timestamp}

所以,我需要结果

对于A,
A和B之间的关系是di,具有特定的时间戳t1
此外,它与C之间的关系是o,具有特定的时间戳t2

我为每项活动都需要这个。

但是代码的输出出现了

A {B = [o,specific timestamp,di,specfic timestamp] , C = [o,specific timestamp,di,specfic timestamp]}

但我需要输出

A {B = [di,specfic timestamp] , C = [o,specific timestamp]}

我该怎么办?

ArrayList<Object> fill_data = new ArrayList<Object>();
public void activityRelationShip(ArrayList<Eventlog> data, ArrayList<String> activities) throws SQLException, ClassNotFoundException, InstantiationException, IOException {
    Map<String, Map<String, ArrayList<Object>>> map = new HashMap<String, Map<String, ArrayList<Object>>>();


    for (int i = 0; i < activities.size(); i++) {
        map.put(activities.get(i), new HashMap<String, ArrayList<Object>>());
    }
    ArrayList<Object> s = new ArrayList<Object>();
    int index = 0;
    boolean flag = false;
    // here we will add corrsponding activities and relation between them and also timestamp
    for (int j = 0; j < activities.size(); j++) {
        index = 0;
        for (int y = 0; y < data.size(); y++) {
            if (data.get(y).getActivity().equalsIgnoreCase(activities.get(j)) == true && data.get(y).getAbstractState().equalsIgnoreCase("Active") == true) {
                index = y;
                break;
            }
        }

        for (int k = index; k < data.size(); k++) {
            if (data.get(k).getActivity().equalsIgnoreCase(activities.get(j)) == false) {
                if (data.get(k).getAbstractState().equalsIgnoreCase("Active") == true) {
                    for (int i = k + 1; i < data.size(); i++) {
                        System.out.println(data.get(i).getActivity() + " -- > " + data.get(k).getActivity());
                        if (data.get(i).getActivity().equalsIgnoreCase(data.get(k).getActivity()) == true
                                && data.get(i).getAbstractState().equalsIgnoreCase("Idle") == true) {
                            map.get(activities.get(j)).put(data.get(k).getActivity(), fill_data);
                            flag = true;
                            break;
                        } else if (data.get(i).getActivity().equalsIgnoreCase(activities.get(j)) == true && data.get(i).getAbstractState().equalsIgnoreCase("Idle") == true) {
                            break;
                        }
                    }
                    if (flag == true) {
                        flag =false;
                        map.get(activities.get(j)).get(data.get(k).getActivity()).add("di");
                        map.get(activities.get(j)).get(data.get(k).getActivity()).add(data.get(k).getTime());
                    } else {
                        map.get(activities.get(j)).put(data.get(k).getActivity(), fill_data);
                        map.get(activities.get(j)).get(data.get(k).getActivity()).add("o");
                        map.get(activities.get(j)).put(data.get(k).getActivity(), fill_data);
                        map.get(activities.get(j)).get(data.get(k).getActivity()).add(data.get(k).getTime());
                    }
                } // close if "Active"
            } else if (data.get(k).getActivity().equalsIgnoreCase(activities.get(j)) == true && data.get(k).getAbstractState().equalsIgnoreCase("Idle")) {
                break;
            }

            s = map.get(activities.get(j)).get(data.get(k).getActivity());
        }
        if (s != null) {
            System.out.println("Entry set " + map.get(activities.get(j)).entrySet());
        }
    }

0 个答案:

没有答案