I have an array which I want to convert to Map such that the first element in the array is key and second element is value and so on.
I am trying something like below:
Arrays.asList("Tamilnadu", "chennai", "Karnataka", "Bengluru")
.stream()
.collect(HashMap::new,
(a, b) -> a.put(b, ""),//value should be next element in array
(a, b) -> a.putAll(b)
);
My confusion is how to get next element in same collect iteration so that I could do a.put(b,nextElementToB)
.
Can someone help with this problem by using streams as I want to try functional programming for same? Is iterating going to be the only way of doing this?