[英]Java1.8 Split string into key-value pairs
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. Now:表示键值对,而,分隔键值对。 I want to add the key-value pairs to a HashMap. I am expecting output:-我想将键值对添加到 HashMap。我期待 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}
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 .请参阅在 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.正如我在评论中所说,由于重复键,您不能使用单个 map。 You may want to consider a class as follows to hold the information您可能需要考虑如下 class 来保存信息
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 中拥有数据的给定国家/地区的所有城市分组,如下所示:
Map<String, List<CityData>>
where the Key is the country. Map<String, List<CityData>>
其中 Key 是国家。
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} output:{创建=2022-03-16T07:10:26.135Z,时间戳=2022-03-16T07:10:26.087Z}
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.您需要拆分数据并对此进行迭代,通过指定 index=2 在冒号上再拆分一次并将结果存储在 Map 中。如果要保留顺序,请使用 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} Output: {created=2022-03-16T07:10:26.135Z, timestamp=2022-03-16T07:10:26.087Z, city=Bangalore, Country=Ind}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.