java gt lt,Java 8:如何转换List< String>到Map< String,List< String>&gt ;?

I have a List of String like:

List locations = Arrays.asList("US:5423","US:6321","CA:1326","AU:5631");

And I want to convert in Map> as like:

AU = [5631]

CA = [1326]

US = [5423, 6321]

I have tried this code and it works but in this case, I have to create a new class GeoLocation.java.

List locations=Arrays.asList("US:5423", "US:6321", "CA:1326", "AU:5631");

Map> locationMap = locations

.stream()

.map(s -> new GeoLocation(s.split(":")[0], s.split(":")[1]))

.collect(

Collectors.groupingBy(GeoLocation::getCountry,

Collectors.mapping(GeoLocation::getLocation, Collectors.toList()))

);

locationMap.forEach((key, value) -> System.out.println(key + " = " + value));

GeoLocation.java

private class GeoLocation {

private String country;

private String location;

public GeoLocation(String country, String location) {

this.country = country;

this.location = location;

}

public String getCountry() {

return country;

}

public void setCountry(String country) {

this.country = country;

}

public String getLocation() {

return location;

}

public void setLocation(String location) {

this.location = location;

}

}

But I want to know, Is there any way to convert List to Map> without introducing new class.

解决方案

You may do it like so:

Map> locationMap = locations.stream()

.map(s -> s.split(":"))

.collect(Collectors.groupingBy(a -> a[0],

Collectors.mapping(a -> a[1], Collectors.toList())));

A much more better approach would be,

private static final Pattern DELIMITER = Pattern.compile(":");

Map> locationMap = locations.stream()

.map(s -> DELIMITER.splitAsStream(s).toArray(String[]::new))

.collect(Collectors.groupingBy(a -> a[0],

Collectors.mapping(a -> a[1], Collectors.toList())));

Update

As per the following comment, this can be further simplified to,

Map> locationMap = locations.stream().map(DELIMITER::split)

.collect(Collectors.groupingBy(a -> a[0],

Collectors.mapping(a -> a[1], Collectors.toList())));

Java中的集合List是一种常用的数据结构,它可以存储多个对象并且可以按照一定的顺序进行访问。而Map则是一种键值对的映射表,它也可以存储多个对象,但是访问的方式是通过键而不是索引。有时候,我们需要将一个List集合中的多个Map对象转换成一个List,这时可以通过遍历List集合中的Map对象,将每个Map中的value值取出来,然后添加到新的List中。 具体流程如下: 1. 创建一个新的List对象,用于存储Map对象的value值。 2. 遍历List集合中的Map对象,获取Map的value值。 3. 将value值添加到新的List中。 4. 返回新的List对象即可。 下面是示例代码: ``` // 创建一个List<Map<String, Object>>集合 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); // 添加多个Map对象到ListMap<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "张三"); map1.put("age", 20); list.add(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", "李四"); map2.put("age", 21); list.add(map2); // 将List集合中的Map对象List List<Object> resultList = new ArrayList<Object>(); for (Map<String, Object> map : list) { resultList.add(map.get("name")); resultList.add(map.get("age")); } // 输出新的List对象 System.out.println(resultList); ``` 以上代码运行结果为:[张三, 20, 李四, 21]。 注意:在实际应用中,如果List集合中的Map对象的属性名和属性值类型不同,需要根据具体情况进行适当的数据类型转换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值