3

I have to split array data ,make it key value pair and save in a map,This is code using for split and save in subdate ,after that in data please clear this

Map<Integer, Map<String, String>> data = new HashMap<Integer, Map<String, String>>();
Map<String, String> subData = new HashMap<String, String>();
CLog.d("",""+fsplit.length);
for (int j = 1; j <=(fsplit.length/3); j++) {
    CLog.d("fsplitsixe", "" + fsplit.length);

           for (String test : fsplit) {
               String s = test;
               String[] parts = s.split("=");
               if (parts.length == 2) {
                   subData.put(parts[0], parts[1]);
               }
           }
    CLog.d("subsizetest",""+subData.size());
    data.put(j, subData);
    CLog.d(TAG, "MAKE_KEY_PAIR" + data);
}

My Array Data in fsplit is this :

0. url=http://www.krak.dk//53504900/s%C3%B8g.cs
1. datasource=ENIRODK_YELLOW_DATA
2. matchstring=hit-list
3. url=http://www.krak.dk/person/resultat/53504900
4. datasource=ENIRODK_WHITE_DATA
5. matchstring=hit-list

excepted output is:

MAKE_KEY_PAIR {
    1 = {
        matchstring = hit-list,
        datasource = ENIRODK_YELLOW_DATA,
        url = http://www.krak.dk//53504900/s%C3%B8g.cs
    },
    2 = {
        matchstring = hit-list,
        datasource = ENIRODK_WHITE_DATA,
        url = http://www.krak.dk/person/resultat/53504900
    }
}

But my output :

MAKE_KEY_PAIR {
    1 = {
        matchstring = hit-list,
        datasource = ENIRODK_WHITE_DATA,
        url = http://www.krak.dk/person/resultat/53504900
    },
    2 = {
        matchstring = hit-list,
        datasource = ENIRODK_WHITE_DATA,
        url = http://www.krak.dk/person/resultat/53504900
    }
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Ashwa
  • 41
  • 1
  • 10

2 Answers2

2

Try below code:

Map<Integer, Map<String, String>> data = new LinkedHashMap<Integer, Map<String, String>>();
Map<String, String> subData;
for (int j= 0; j <fsplit.length;) {
    subData = new HashMap<String, String>();
    int i=0;
    while(i<3){
        String s = fsplit[j++];
        String[] parts = s.split("=");
        if (parts.length == 2) {
            subData.put(parts[0], parts[1]);
        }
        i++;
    }
    data.put(j/3, subData);
}
  1. subData Map has same data:
    As you are using same subData Map, existing value is replace with new value, where key is same for both.

  2. data Map has same data:
    As you are iterating same fsplit array multiple time, always last 3 value is get added into data map.

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
0

Read the comments too

foreach loop Always pick the string content from beginning of the string(fsplit). After getting first result you are not updating fsplit. fsplit is a Object of String Class which is immutable. So update the content of fsplit after getting your result at each iteration

for (int j = 1; j <=(fsplit.length/3); j++) {
    CLog.d("fsplitsixe", "" + fsplit.length);
    for (String test : fsplit) {  

        /* this line always pick the string content from beginning of the string(fsplit). 
        After gettingfirst result you are not updating fsplit. 
        fsplit is a `Object` of `String` Class which is immutable. 
        So update the content of fsplit after getting your result 
        at each iteration */

        String s = test;
        String[] parts = s.split("=");
        if (parts.length == 2) {
        subData.put(parts[0], parts[1]);
    }
}
Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52