我需要一个TreeMap,其中的ID将是ID和值(每个ID数据将是hasMap,该ID将是ID,值将是其余详细信息)吗?

时间:2018-11-04 08:08:04

标签: java

TreeMap<String, HashMap<String, String>> myData = new TreeMap<>();

    HashMap<String, String> data = new HashMap<>(); //data present in this map will be name as key & Values will rest other details
    HashMap<String, String> lock = new HashMap<>(); //data present in this map Will be id as Key & Values will be name+Email

Iterator itr = lock.entrySet().iterator();
        Iterator trans = data.entrySet().iterator();
        HashMap<String, String> vessel = new HashMap<>(); //Temp Holding of EachKey & value of name as key and rest as values
        TreeMap demo=new TreeMap();
        while (itr.hasNext()) {
            Entry e = (Entry) itr.next();
            String key = (String) e.getKey();
            name = ReadExcel.getString((String) e.getValue(), 0);
            String email = ReadExcel.getString((String) e.getValue(), 1);
            while (trans.hasNext()) {
                vessel.clear();
                Entry ex = (Entry) trans.next();
                String NameVs = (String) ex.getKey();
                String emailVs = ReadExcel.getString((String) ex.getValue(), 0);
                if (name.equalsIgnoreCase(NameVs) && email.equalsIgnoreCase(emailVs)) {
                    vessel.put(NameVs, data.get(NameVs));
                    //System.err.println(vessel.values());
                    myData.put(key, vessel);    //Putting data in treeMap 
                    System.out.println(myData.entrySet());
                    break;
                }
            }
            trans = data.entrySet().iterator();
        }   

需要输出为

  

[1 = {RAM=aa@gmail.com Domulur 7894561230},2={ANIL=xyZ@gmail.com MUM 8464648848}]。如果我有一些数据作为“ ID名称电子邮件地址暴民”。但是问题是如果我使用clear()数据替换为最后一行数据。例如,[1 = {ANIL=xyZ@gmail.com MUM 8464648848},2={ANIL=xyZ@gmail.com MUM 8464648848} ],如果我不使用clear(),则将附加值。例如,[1 = {RAM=aa@gmail.com Domulur 7894561230,ANIL = xyZ @ gmail.com MUM 8464648848},2={RAM=aa@gmail.com Domulur 7894561230,ANIL = xyZ @ gmail.com MUM 8464648848 }]。

需要帮助谢谢。

1 个答案:

答案 0 :(得分:0)

您为HashMap<String, String>的所有键使用相同的TreeMap实例作为值,因此,当然所有键都具有相同的值。

您应该为每个键创建一个新的HashMap

    ....
    while (itr.hasNext()) {
        Entry e = (Entry) itr.next();
        String key = (String) e.getKey();
        name = ReadExcel.getString((String) e.getValue(), 0);
        String email = ReadExcel.getString((String) e.getValue(), 1);
        while (trans.hasNext()) {
            HashMap<String, String> vessel = new HashMap<>();
            Entry ex = (Entry) trans.next();
            String NameVs = (String) ex.getKey();
            String emailVs = ReadExcel.getString((String) ex.getValue(), 0);
            if (name.equalsIgnoreCase(NameVs) && email.equalsIgnoreCase(emailVs)) {
                vessel.put(NameVs, data.get(NameVs));
                myData.put(key, vessel);    //Putting data in treeMap 
                break;
            }
        }
        trans = data.entrySet().iterator();
    }  
    ....