Java中,将 List<Object> 转换为 Map<String, Map<String, List<Object>>> 可以通过多种方法实现,具体取决于如何从 Object 中提取 String 键和 List<Object> 的结构。本文主要介绍Java中将指定List<Object>类型数据转换成Map<String, Map<String,List<Object>>>类型的几种方法。通过stream()或foreach循环实现。

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

推荐文档

相关文档

大家感兴趣的内容

随机列表