java lambda list转map
时间: 2024-05-22 16:08:31 浏览: 16
在Java 8中,我们可以使用Lambda表达式将List转换为Map。以下是一种常见的方法:
假设我们有一个类Person,其中包含两个属性:id和name。我们要将一个Person的List转换为一个Map,其中id作为key,name作为value。
首先,我们需要将List转换为Stream。然后,我们可以使用Collectors.toMap()方法将每个Person对象转换为一个Map.Entry对象,并将它们收集到一个Map中。下面是代码示例:
```
List<Person> personList = new ArrayList<>();
personList.add(new Person(1, "Alice"));
personList.add(new Person(2, "Bob"));
personList.add(new Person(3, "Charlie"));
Map<Integer, String> idToNameMap = personList.stream()
.collect(Collectors.toMap(Person::getId, Person::getName));
System.out.println(idToNameMap);
```
输出结果如下:
```
{1=Alice, 2=Bob, 3=Charlie}
```
相关问题
Java lambda list 转map
在Java中,可以使用Lambda表达式将List转换为Map。以下是一个示例代码:
```java
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ListToMapExample {
public static void main(String[] args) {
List<Person> personList = List.of(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Charlie")
);
// 使用Lambda表达式将List转换为Map
Map<Integer, String> personMap = personList.stream()
.collect(Collectors.toMap(Person::getId, Person::getName));
// 打印转换后的Map
System.out.println(personMap);
}
}
class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
```
在上述示例中,我们首先创建了一个Person类,其中包含id和name属性。然后,我们创建了一个包含Person对象的List。使用Java 8的Stream API和Collectors类的toMap方法,我们将List转换为Map。在toMap方法中,我们指定了Person对象的id属性作为键,name属性作为值。最后,我们打印转换后的Map。
请注意,如果List中存在重复的键,toMap方法将引发IllegalStateException异常。可以使用toMap方法的重载版本来处理重复键的情况。
java lambda list转换map时,把多个参数拼接作为key操作
Java 8 引入的 lambda 表达式和 Stream API 大大简化了我们对集合的操作。其中,Stream 的 map() 函数可以将一个集合转换为另一个集合,非常方便。而在将一个 List 转换为 Map 时,我们有时需要将多个参数拼接成一个字符串作为键(Key)。这种情况下,我们可以使用 lambda 表达式提供的功能将多个参数拼接起来。
具体来说,我们可以使用 Stream API 的 collect() 函数,并传入一个 Collector 对象。而当我们想要将多个参数拼接作为 Key 的时候,我们可以使用 Collectors 的 toMap() 方法,并重载该方法传入一个 Function 对象来实现。这个 Function 对象可以接受多个参数,并将它们拼接起来作为 Key。
比如,我们有一个装有 Person 类型的 List,每个 Person 包含了 name 和 age 两个属性,我们想要根据 name 和 age 生成一个 Map,其中 Key 为 name + "#" + age,Value 为 Person 实例。我们可以这样做:
```
List<Person> personList = ... ;
Map<String, Person> personMap = personList.stream()
.collect(Collectors.toMap(
person -> person.getName() + "#" + person.getAge(),
person -> person));
```
这样,我们就得到了一个由 name+“#”+age 组成的 String 作为 Key,对应的 Value 是 Person 实例。通过这样的方式,我们可以很方便地将多个参数组合成一个字符串作为 Map 的 Key,并根据此生成 Map。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)