Converting string array to hashmap

70,515

Solution 1

final String[] fields = input.split("\\|");
final Map<String, String> m = new HashMap<String, String>();
int i = 0;
for (String key : new String[] {"id", "cid", "refno"})
  m.put(key, fields[i++]);

Solution 2

You have to loop and add the results one by one. Declare an array with the keys, something like:

static String[] keys = new String[]{"id", "cid", "refno", ...};

and then

String[] s = text.split("\\|");
for (int i = 0; i < s.length; i++)
  map.put(keys[i], s[i]);
Share:
70,515
Daya
Author by

Daya

Works for Infosys, Bangalor .

Updated on July 09, 2022

Comments

  • Daya
    Daya almost 2 years

    I have the following response

    T2269|175@@2a1d2d89aa96ddd6|45464047
    

    By using the split("\\|") i have converted into string array object. The meaning for the each field is as follows:

    T2269                  id
    175@@2a1d2d89aa96ddd6  cid
    45464047               refno
    

    No i have to convert it into HashMap object . Is their any solution for the above..

    The above response is given for example. In real, the length of the string array object is 36.