1

I have a string like this but very big string

String data = "created:2022-03-16T07:10:26.135Z,timestamp:2022-03-16T07:10:26.087Z,city:Bangalore,Country:Ind";

Now : indicates key-value pairs while , separates the pairs. I want to add the key-value pairs to a HashMap. I am expecting output:-

{created=2022-03-16T07:10:26.135Z,timestamp=2022-03-16T07:10:26.087Z,city=Bangalore,Country=Ind}

I tried in multiple way but I am getting like that

{timestamp=2022-03-16T07, created=2022-03-16T07}
7
  • 4
    Do you suppose posting the code you used to split your strings might help people see your mistake? If you're asking people for an answer, providing evidence would seem to be the right approach. Trying to force people to guess won't get you anywhere. Commented Mar 17, 2022 at 17:12
  • 1
    : indicates key-value pairs No, not all. Not those between hours, minutes and seconds. Commented Mar 17, 2022 at 17:23
  • You can't use created and timestamp repeatedly in the same map as duplicate keys are not permitted. How to you plan to handle multiple similar strings in the same map and still keep the proper pairings? Commented Mar 17, 2022 at 17:36
  • 1
    Why don't use create a class to hold the information since you can't use a single map because of the duplicate keys. Commented Mar 17, 2022 at 18:40
  • 1
    I have a string like this but very big string. Please explain that statement. Does that very big string represent multiple cities and countries? Commented Mar 17, 2022 at 23:06

3 Answers 3

3

Based on the information provided, here one way to do it. It required both splitting in sections and limiting the size and location of the split.

String data = "created:2022-03-16T07:10:26.135Z,timestamp:2022-03-16T07:10:26.087Z,city:Bangalore,Country:Ind";

Map<String, String> map =
        Arrays.stream(data.split(","))
        .map(str -> str.split(":", 2))
        .collect(Collectors.toMap(a -> a[0], a -> a[1]));

map.entrySet().forEach(System.out::println);        

See this code run live at IdeOne.com.

city=Bangalore
created=2022-03-16T07:10:26.135Z
Country=Ind
timestamp=2022-03-16T07:10:26.087Z

As I said in the comments, you can't use a single map because of the duplicate keys. You may want to consider a class as follows to hold the information

class CityData {
    private String created;  // or a ZonedDateTime instance
    private String timeStamp;// or a ZonedDateTime instance
    private String city;
    private String country;
    @Getters and @setters
}

You could then group all the cities for of a given country for which you had data in a map as follows:

Map<String, List<CityData>> where the Key is the country.

Sign up to request clarification or add additional context in comments.

Comments

0
var data="created:2022-03-16T07:10:26.135Z,timestamp:2022-03-16T07:10:26.087Z";

var split = data.split(","); // splitting created and timestamp
var created = split[0].substring(8); // 8 is size of 'created:'
var timestamp = split[1].substring(10); // 10 is size of 'timestamp:'

Map<String, String> result = new HashMap<>();
result.put("created", created);
result.put("timestamp", timestamp);

output: {created=2022-03-16T07:10:26.135Z, timestamp=2022-03-16T07:10:26.087Z}

3 Comments

We have very long string like that substring will not work.
@kundansingh ok, the problem is you don't have a string in JSON format if it was then you can use ObjectMapper.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You need to split the data and iterate on this, split it one more time on colon by specifying index=2 and store the result in a Map. If you want to preserve the order use LinkedHashMap.

Map<String, String> map = new LinkedHashMap<>();
String data = "created:2022-03-16T07:10:26.135Z,timestamp:2022-03-16T07:10:26.087Z,city:Bangalore,Country:Ind";

String[] split = data.split(",");
for (String str: split) {
    String[] pair = str.split(":", 2);
    map.put(pair[0],pair[1]);
}

System.out.println(map);

Output: {created=2022-03-16T07:10:26.135Z, timestamp=2022-03-16T07:10:26.087Z, city=Bangalore, Country=Ind}

1 Comment

Please make sure to add your own code in the question. stackoverflow.com/help/minimal-reproducible-example also please accept the answer. Thanks.

Your Answer

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