7

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
Daya
  • 724
  • 3
  • 14
  • 32
  • 4
    What would be the keys and the values in your map? Do you mean a map with three entries (id = T2269, cid = 175..., refno = 4546...) ? Or a map where the values are objects with three fields (id, cid, refno)? In that case what would be the key? – assylias Jul 05 '12 at 08:29

3 Answers3

11

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]);
tibtof
  • 7,857
  • 1
  • 32
  • 49
10
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++]);
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

The key should be unique, so obviously using the ID as a key would make perfect sense, and the value you can store as an array/list containing the id,cid,refno, or you can create an object containing thos fields and store it.

Tomer
  • 17,787
  • 15
  • 78
  • 137