将HashMap放入HashMap中,然后填充两个映射。

时间:2013-07-23 12:53:35

标签: java hashmap

我必须为sql插件编写一个脚本。我拥有编写插入内容所需的所有信息,但我使用的信息有三种,而不仅仅是两种。我知道我必须在Map中放置一个Map,但是当我尝试将任何东西放入地图时,我收到编译错误。

HashMap<String,String> acctyp = new HashMap<String,String>();
HashMap<String, HashMap<String,String>> gens = new HashMap<String,HashMap<String,String>>();
gens.put("'GEN01'", acctyp.put("'Open'","ACCTYP 01"));
gens.put("'GEN02'", acctyp.put("'Sheltered'","ACCTYP 02"));
gens.put("'GEN03'", acctyp.put("'Spousal sheltered'","ACCTYP 03"));
gens.put("'GEN06'", acctyp.put("'Education'","ACCTYP 06"));
gens.put("'GEN12'", acctyp.put("'Non-ded. sheltered'","ACCTYP 12"));
gens.put("'GEN30'", acctyp.put("'Structured Product'","ACCTYP 30"));
gens.put("'GEN31'", acctyp.put("'Emergency Cash'","ACCTYP 31"));
gens.put("'GEN85'", acctyp.put("'Insurance Savings'","ACCTYP 85"));
gens.put("'GEN86'", acctyp.put("'Stock Option'","ACCTYP 86"));
gens.put("'GEN94'", acctyp.put("'Business Other'","ACCTYP 94"));
gens.put("'GEN95'", acctyp.put("'Personal use'","ACCTYP 95"));
gens.put("'GEN97'", acctyp.put("'Universal Life'","ACCTYP 97"));

如果有人对我为什么会在看跌期权上遇到编译错误有任何见解,那么我们将不胜感激。

6 个答案:

答案 0 :(得分:14)

Map.put返回一个字符串。 gens正在将String映射到HashMap。您正在致电gens.put(string, string),而应致电gens.put(string, HashMap<string,string>)

您的代码应为:

HashMap<String,String> acctyp = new HashMap<String,String>();
HashMap<String, HashMap<String,String>> gens = new HashMap<String,HashMap<String,String>>();
acctyp.put("'Open'","ACCTYP 01");
gens.put("'GEN01'", acctyp);

答案 1 :(得分:2)

因为Map#put()返回String值。执行此操作时:acctyp.put("'Open'","ACCTYP 01"),它实际返回String,因此gens.put("'GEN01'", acctyp.put("'Open'","ACCTYP 01"));是编译错误。

我想你想要的是:

HashMap<String,String> acctyp = new HashMap<String,String>();
HashMap<String, HashMap<String,String>> gens = new 
HashMap<String,HashMap<String,String>>();
acctyp.put("'Open'","ACCTYP 01");
acctyp.put("'Sheltered'","ACCTYP 02");
acctyp.put("'Spousal sheltered'","ACCTYP 03");
gens.put("'GEN01'", acctype);

答案 2 :(得分:2)

您的代码应该是这样的

        HashMap<String, HashMap<String,String>> gens = new HashMap<String,HashMap<String,String>>();
        HashMap<String,String> acctyp =  new HashMap<String,String>();
        acctyp.put("'Open'","ACCTYP 01");
        gens.put("'GEN01'", acctyp);
        acctyp =  new HashMap<String,String>();
        acctyp.put("'Sheltered'","ACCTYP 02");
        gens.put("'GEN02'", acctyp);
        acctyp =  new HashMap<String,String>();
        acctyp.put("'Spousal sheltered'","ACCTYP 03");
        gens.put("'GEN03'",acctyp );
        acctyp =  new HashMap<String,String>();
        acctyp.put("'Education'","ACCTYP 06");
        gens.put("'GEN06'",acctyp );            
        acctyp =  new HashMap<String,String>();
        acctyp.put("'Non-ded. sheltered'","ACCTYP 12");
        gens.put("'GEN06'",acctyp );
        acctyp =  new HashMap<String,String>();
        acctyp.put("'Structured Product'","ACCTYP 30");
        gens.put("'GEN06'",acctyp );
        gens.put("'GEN31'",);
        acctyp =  new HashMap<String,String>();
        acctyp.put("'Emergency Cash'","ACCTYP 31");
        gens.put("'GEN06'",acctyp );

答案 3 :(得分:1)

如果您想稍后填写第二个哈希映射,也可以编写此代码:

acctype.put("'Open'",new Hashmap<String,String>()); 
acctype.put("''Sheltered'",new Hashmap<String,String>());
... So on
actttype.get("'Open'").put("'Sheltered'","ACCTYP 02")
acttype.get("'Sheltered'").put("'Spousal sheltered'","ACCTYP 03")
and so on ...

希望也有帮助

答案 4 :(得分:0)

 - I think u should do like this way..

   HashMap<String,String> acctyp = new HashMap<String,String>();
   HashMap<String, HashMap<String,String>> gens = new
   HashMap<String,HashMap<String,String>>();

    acctyp.put("'Open'","ACCTYP 01");
           gens.put("'GEN01'", acctyp);
           acctyp.put("'Sheltered'","ACCTYP 02");
           gens.put("'GEN02'", acctyp);
           acctyp.put("'Spousal sheltered'","ACCTYP 03");
           gens.put("'GEN03'",acctyp );

答案 5 :(得分:0)

static Map>divisons=newHashMap>();

public static void main(String[] args) {
    Map < String, Integer > studentMarksA = new HashMap < String, Integer > ();
    Map < String, Integer > studentMarksB = new HashMap < String, Integer > ();
    Map < String, Integer > studentMarksC = new HashMap < String, Integer > ();
    // Entering for marks for divison A
    divisons.put("9A", studentMarksA);
    studentMarksA.put("Agniv", 60);
    studentMarksA.put("Bhuvan", 70);
    studentMarksA.put("Chandan", 80);
    // Entering for marks for divison B
    divisons.put("9B", studentMarksB);
    studentMarksB.put("Dev", 50);
    studentMarksB.put("Ejazd", 90);
    studentMarksB.put("Faruk", 61);
    // Entering for marks for divison C
    divisons.put("9C", studentMarksC);
    studentMarksC.put("Govind", 68);
    studentMarksC.put("Harish", 71);
    studentMarksC.put("Ishwar", 84);

    getRecord("B");
}