-1

I need to convert a String to List<Map<String, String>>> for pass JUnit Test. I have this:

String userAttributes = "[{name=test, cp=458999, lastname=test2}]";

That i want is in the tests (Mockito) change a call to a server with this values, something like this:

Mockito.when(template.search(Mockito.anyString, new AttributesMapper()).thenReturn(attributes);

I need List<Map<String, String>>> for do this:

user.setUserName(attributes.get("name"));
3
  • 1
    Possible duplicate of How to convert String into Hashmap in java Commented Oct 25, 2019 at 10:19
  • Provide an answer instead of editing your question to add a SOLUTION section. Commented Oct 25, 2019 at 11:41
  • Can you say if given ansvers solve your problem or say what's wrong are with them? Respond on answers helps in future to precise the answers. Commented Oct 28, 2019 at 16:56

2 Answers 2

0

Try regex or split by special char. First remove parentheses from beginning and end. After that you can split it by , and = to collect strings to map.

String userAttributes = "[{name=test, cp=458999, lastname=test2}]";

List<String> strings = Arrays.asList(userAttributes
      .replace("[{","").replace("}]","")
      .split(", "));
Map<String, String> collect = strings.stream()
      .map(s -> s.split("="))
      .collect(Collectors.toMap(s -> s[0], s -> s[1]));

System.out.println(collect.get("name"));

Other approach with Pattern

Map<String, String> collect = Pattern.compile(",")
        .splitAsStream(userAttributes
                .replace("[{","").replace("}]",""))
        .map(s -> s.split("="))
        .collect(Collectors.toMap(s -> s[0], s -> s[1]));

Or if you really wants use List<Map<String, String>>>. But after that you can not do this user.setUserName(attributes.get("name"));

List<Map<String, String>> maps = strings.stream()
      .map(s -> s.split("="))
      .map(s -> Map.of(s[0], s[1]))
      .collect(Collectors.toList());

System.out.println(maps);
Sign up to request clarification or add additional context in comments.

Comments

-1
    String userAttributes = "[{name=test, cp=458999, lastname=test2}]";
    StringTokenizer stringTokenizer = new StringTokenizer(userAttributes,",");
    List<Map<String,String>> list = new ArrayList<>();
    while(stringTokenizer.hasMoreElements()){
        StringTokenizer stringTokenizer2 = new StringTokenizer((String)stringTokenizer.nextElement(),"=");
        while(stringTokenizer2.hasMoreElements()){
            Map<String, String> map = new HashMap<>();
            map.put( ((String)stringTokenizer2.nextElement()),((String)stringTokenizer2.nextElement()) );
            list.add(map);
        }
    }

    System.err.println(list.toString());

4 Comments

You do not remove "[{" and "}]"
it is not required if you use StringTokenizer .
Run this code and: [{[{name=test}, { cp=458999}, { lastname=test2}]}]
Or list.stream().forEach(m -> System.out.println(m.entrySet())); you get key "{name" , " cp", " lastname"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.