Java1.8 将字符串拆分为键值对

发布于 2025-01-14 23:30:08 字数 445 浏览 1 评论 0 原文

我有一个像这样的字符串,但是非常大的字符串

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

Now : 表示键值对,而 , 分隔键值对。我想将键值对添加到 HashMap 中。我期待输出:-

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

我尝试了多种方式,但我得到了这样的结果

{timestamp=2022-03-16T07, created=2022-03-16T07}

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活雷疯 2025-01-21 23:30:09

根据提供的信息,这是一种方法。它需要分割成多个部分并限制分割的大小和位置。

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);        

请参阅在 IdeOne.com 上实时运行的代码

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

正如我在评论中所说,由于重复的键,您不能使用单个映射。您可能需要考虑如下的类来保存信息,

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
}

然后您可以将地图中具有数据的给定国家/地区的所有城市分组,如下所示:

Map>< /code> 其中键是国家/地区。

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.

你是年少的欢喜 2025-01-21 23:30:09
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);

输出:
{创建=2022-03-16T07:10:26.135Z,时间戳=2022-03-16T07:10:26.087Z}

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}

你是我的挚爱i 2025-01-21 23:30:09

您需要拆分数据并对其进行迭代,通过指定 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);

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

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}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文