1、使用stream()进行转换
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static Map<String, Map<String, List<Person>>> convertListToMap(List<Person> list) {
return list.stream()
.collect(Collectors.groupingBy(
Person::getPersonId,
Collectors.groupingBy(Person::getType)
));
}
public static void main(String[] args) {
List<Person> list = Arrays.asList(
new Person("1", LocalDate.of(2023, 8, 1), "A"),
new Person("1", LocalDate.of(2023, 8, 2), "B"),
new Person("2", LocalDate.of(2023, 8, 1), "A"),
new Person("1", LocalDate.of(2023, 8, 3), "A")
);
Map<String, Map<String, List<Person>>> result = convertListToMap(list);
System.out.println(result);
}
}
class Person {
private String personId;
private LocalDate date;
private String type;
public Person(String personId, LocalDate date, String type) {
this.personId = personId;
this.date = date;
this.type = type;
}
public String getPersonId() {
return personId;
}
public LocalDate getDate() {
return date;
}
public String getType() {
return type;
}
}
2、自定义逻辑和Lambda表达式结合
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static Map<String, Map<String, List<Person>>> convertListToMap(List<Person> list) {
return list.stream()
.collect(Collectors.groupingBy(
person -> customFirstKeyFunction(person),
Collectors.groupingBy(person -> customSecondKeyFunction(person))
));
}
private static String customFirstKeyFunction(Person person) {
// 自定义第一层key的逻辑
return person.getPersonId();
}
private static String customSecondKeyFunction(Person person) {
// 自定义第二层key的逻辑
return person.getType();
}
public static void main(String[] args) {
List<Person> list = Arrays.asList(
new Person("1", LocalDate.of(2023, 8, 1), "A"),
new Person("1", LocalDate.of(2023, 8, 2), "B"),
new Person("2", LocalDate.of(2023, 8, 1), "A"),
new Person("1", LocalDate.of(2023, 8, 3), "A")
);
Map<String, Map<String, List<Person>>> result = convertListToMap(list);
System.out.println(result);
}
}
class Person {
private String personId;
private LocalDate date;
private String type;
public Person(String personId, LocalDate date, String type) {
this.personId = personId;
this.date = date;
this.type = type;
}
public String getPersonId() {
return personId;
}
public LocalDate getDate() {
return date;
}
public String getType() {
return type;
}
}
3、使用for循环转换
import java.time.LocalDate;
import java.util.*;
public class Main {
public static Map<String, Map<String, List<Person>>> convertListToMap(List<Person> list) {
Map<String, Map<String, List<Person>>> resultMap = new HashMap<>();
for (Person person : list) {
String personId = person.getPersonId();
String type = person.getType();
resultMap
.computeIfAbsent(personId, k -> new HashMap<>())
.computeIfAbsent(type, k -> new ArrayList<>())
.add(person);
}
return resultMap;
}
public static void main(String[] args) {
List<Person> list = Arrays.asList(
new Person("1", LocalDate.of(2023, 8, 1), "A"),
new Person("1", LocalDate.of(2023, 8, 2), "B"),
new Person("2", LocalDate.of(2023, 8, 1), "A"),
new Person("1", LocalDate.of(2023, 8, 3), "A")
);
Map<String, Map<String, List<Person>>> result = convertListToMap(list);
System.out.println(result);
}
}
class Person {
private String personId;
private LocalDate date;
private String type;
public Person(String personId, LocalDate date, String type) {
this.personId = personId;
this.date = date;
this.type = type;
}
public String getPersonId() {
return personId;
}
public LocalDate getDate() {
return date;
}
public String getType() {
return type;
}
}