I am getting some issues to convert my data to map, here are my data:
For e.g
[
[abc, pqr, xyz],
[1, 2, 3],
[4, 5, 6],
[1, 7, 8]
]
And i have to convert it into map as follow:
{
1: [
{abc:1, pqr:2,xyz:3},
{abc:1, pqr:7,xyz:8}
],
4: [
{abc:4, pqr:5,xyz:6}
]
}
With basic for I know how to do it but with streaming, I am not getting it as I tried to do it with flatMap, reduce methods with a stream but maybe I am doing something very much wrong. So please can anyone help me out with this?
I tried something to flatMap and all as follow but stuck what to do:
List<List<Object>> dd = new ArrayList<List<Object>>();
final List<Object> dd1 = new ArrayList<Object>();
dd1.add("abc");
dd1.add("pqr");
dd1.add("xyz");
dd.add(dd1);
List<Object> dd2 = new ArrayList<Object>();
dd2.add("1");
dd2.add("2");
dd2.add("3");
dd.add(dd2);
dd2 = new ArrayList<Object>();
dd2.add("4");
dd2.add("5");
dd2.add("6");
dd.add(dd2);
Map<String, Object> m = dd.stream().collect(Collectors.toMap(s -> (String) s.get(0), s -> s));
System.out.println(m);
Map<String, Object> m1 = dd.stream().reduce((l1,l2) -> {
return (new Map<String, Object>()).put(dd1.get(l2), l2);
}).orElse(new Map<String, Object>());
There are syntax errors I know in above code. Also, give some good links where can learn to stream nicely.