I have a following String. I want to convert it into List of Map as below,
String esConnectionPropertiesStr = "ID1, 701, REST, 0, $PROJECT_ID),\n" +
"ID2, 702, ES_USERNAME, 0, $PROJECT_ID),\n" +
"ID3, 703, ES_PASSWORD, 0, $PROJECT_ID),\n" +
"ID4, 704, ES_HOST, 0, $PROJECT_ID";
Output:
[
{1=ID1, 2=701, 3= ES_USERNAME, 4= 0, 5= $PROJECT_ID},
{1=ID2, 2=702, 3= ES_PASSWORD, 4= 0, 5= $PROJECT_ID},
{1=ID3, 2=703, 3=ES_HOST, 4= 0, 5= $PROJECT_ID},
{1=ID4, 2=704, 3= ES_PORT, 4= 0, 5=$PROJECT_ID}
]
It is spliting by ),
and then by comma to get map elements. I tried following which works,
AtomicInteger index = new AtomicInteger(0);
Arrays.stream(esConnectionPropertiesStr.split("\\),"))
.map(e -> Arrays.stream(e.split(","))
.collect(Collectors.toMap(n1 -> index.incrementAndGet(), s -> s)))
.peek(i -> index.set(0))
.collect(Collectors.toList());
Is there any better way to do this??