I want to Put two arrays value in Map as key-Value

时间:2016-08-31 17:36:09

标签: arrays string linkedhashmap

I want to Put two arrays value in LinkedHashMap as key-value. Here is the snippet that I'm using:

        String[] s = answer.split("\\,");  
        String[] ss = aa.split("\\,");
        System.out.println(ss.length);  -->prints 3 
        System.out.println(s.length);   -->prints 3

What I want is to put s values as Key and ss values as Value in HashMap.

I'm trying to write code.

    for(int i=0;i<s.length;i++){
            for(int j= 0;j<ss.length;j++){
                if(s[i].length()==s[j].length()){
                    testMap.put(s[i], ss[j]);
                }
            }
        }

But unable to Put into Map. What I've done wrong? And I'm using LinkedHashMap to preserve the order of Insertion.

2 个答案:

答案 0 :(得分:0)

Here is the solution:

        for(int i=0;i<s.length;i++){
                testMap.put(s[i], ss[i]);
        }

I just have to change my loop condition to this. Instead of using two for loops. Thanks everybody.

答案 1 :(得分:0)

Use this code, It will add accordingly

String answer = "ID,NAME,VALUES"; String aa = "1,KLAXXON,ROMEO"; String[] s = answer.split("\\,"); String[] ss = aa.split("\\,");

for (int i = 0; i < s.length; i++) { testMap.put(s[i], ss[i]); }

Output: {ID=1, NAME=KLAXXON, VALUES=ROMEO}