Convert String to List of Map

Code_Mode :

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??

ETO :

The AtomicInteger is redundant here. It adds more complexity and more room for failure. Moreover the specification of Stream API does not guarantee the execution of Stream::peek.

This is the naive (though pretty long) solution:

List<Map<Integer, String>> resultMap =
        Arrays.stream(esConnectionPropertiesStr.split("\\),"))
              .map(row -> Arrays.stream(row.split(","))
                                .collect(collectingAndThen(toList(), 
                                         list ->IntStream.range(0, list.size())
                                                         .boxed()
                                                         .collect(toMap(identity(), list::get)))))
              .collect(toList());

Although the solution above works, IMHO it isn't readable. I would extract the list-to-map conversion into a static util method:

class Utils { // 
    public static Map<Integer, String> toIndexedMap(List<String> list) {
        return IntStream.range(0, list.size())
                        .boxed()
                        .collect(toMap(identity(), list::get));
}

then use the utility methods as follow:

List<Map<Integer, String>> result =
        Arrays.stream(esConnectionPropertiesStr.split("\\),"))
              .map(row -> Arrays.stream(row.split(","))
                                .collect(collectingAndThen(toList(), Utils::toIndexedMap)))
              .collect(toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert List<String> to Map<String, String> in Java

Java 8: How to convert List<String> to Map<String,List<String>>?

convert map string string to map string list string - Java 7

java stream convert list of POJO to Map of Map<String, List<POJO>>

Java: how to convert a List<?> to a Map<String,?>

How to Convert List<Map<String, Object>> to List<Map<String, String>>

How to Convert a List Into Map of Map Map<String, Map<String,Object>>

Jackson to convert a json into a map of String and a list

Convert List<Map<String, Object>> to String[][]

Java Convert List<String> to Map<String, String>

Convert a String to Map(String, List([Int,Int]))

Convert Map<String,List<String>> to List<Map<String,String>> in Kotlin

convert String into List/Map using Dart

How to convert list map to json string and json to list map

Convert List<String> to Map<String, Integer>

Convert Map<String, Object> to Map<String, List<Object>>

Convert byte[] to List<Map<String, Object>>

Java 8 Convert List of List of String to Map

How to convert a list of string to map in Scala?

How to convert List<Map<String,Object>> to Map<String, String>?

Convert List<List<TextValue>> to List<Map<String, String>>

How to convert a list of java objects to Map<String, Map<String, String>>

Convert JSON data to List<Map<String, dynamic>>

convert a Stream<QuerySnapshot<Map<String, dynamic>>> to a List?

Kotlin convert csv to map <String, List>

how to convert List<Map<String, String>> to List<Map<String, Map<String, String>>>

How to Convert a Map<String, List<List<String>>> to Map<String, List<String>>

Dart convert Map<dynamic, dynamic> to Map<String, List<String>>

how to convert List<Map<String, String>> to Map<String, List<String>>