java List<Map<String, Object>> 转 List<JSONObject> 的几种方式

目录

方法一:使用传统循环

[方法二:使用 Java 8 的流(Stream)API](#方法二:使用 Java 8 的流(Stream)API)

[方法三:使用 Guava 库](#方法三:使用 Guava 库)

总结


List<Map<String, Object>> 转换为 List<JSONObject> 有多种方法。以下是几种常见的方法,包括使用传统的循环、Java 8 的流(Stream)API 和 Guava 库。

方法一:使用传统循环

复制代码
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // 创建一个示例 List<Map<String, Object>>
        List<Map<String, Object>> originalList = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "John Doe");
        map1.put("age", 30);
        map1.put("isStudent", false);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "Jane Doe");
        map2.put("age", 25);
        map2.put("isStudent", true);

        originalList.add(map1);
        originalList.add(map2);

        // 使用传统循环转换为 List<JSONObject>
        List<JSONObject> convertedList = new ArrayList<>();
        for (Map<String, Object> map : originalList) {
            convertedList.add(new JSONObject(map));
        }

        // 打印结果
        for (JSONObject json : convertedList) {
            System.out.println(json.toString());
        }
    }
}

方法二:使用 Java 8 的流(Stream)API

复制代码
import org.json.JSONObject;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        // 创建一个示例 List<Map<String, Object>>
        List<Map<String, Object>> originalList = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "John Doe");
        map1.put("age", 30);
        map1.put("isStudent", false);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "Jane Doe");
        map2.put("age", 25);
        map2.put("isStudent", true);

        originalList.add(map1);
        originalList.add(map2);

        // 使用 Stream API 转换为 List<JSONObject>
        List<JSONObject> convertedList = originalList.stream()
                .map(JSONObject::new)
                .collect(Collectors.toList());

        // 打印结果
        for (JSONObject json : convertedList) {
            System.out.println(json.toString());
        }
    }
}

方法三:使用 Guava 库

如果你已经在项目中使用了 Guava 库,可以利用 FluentIterableFunctions 来简化转换过程。

首先,确保你已经添加了 Guava 库的依赖。如果你使用的是 Maven,可以在 pom.xml 中添加以下依赖:

复制代码
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

然后,使用 Guava 进行转换:

复制代码
import com.google.common.collect.FluentIterable;
import com.google.common.base.Functions;
import org.json.JSONObject;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // 创建一个示例 List<Map<String, Object>>
        List<Map<String, Object>> originalList = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "John Doe");
        map1.put("age", 30);
        map1.put("isStudent", false);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "Jane Doe");
        map2.put("age", 25);
        map2.put("isStudent", true);

        originalList.add(map1);
        originalList.add(map2);

        // 使用 Guava 转换为 List<JSONObject>
        List<JSONObject> convertedList = FluentIterable.from(originalList)
                .transform(Functions.<Map<String, Object>>identity().compose(JSONObject::new))
                .toList();

        // 打印结果
        for (JSONObject json : convertedList) {
            System.out.println(json.toString());
        }
    }
}

总结

  1. 方法一:使用传统循环

    • 创建一个空的 List<JSONObject>
    • 遍历 List<Map<String, Object>>,将每个 Map 转换为 JSONObject 并添加到新的列表中。
  2. 方法二:使用 Java 8 的流(Stream)API

    • 使用 stream() 方法创建一个流。
    • 使用 map(JSONObject::new) 将每个 Map 转换为 JSONObject
    • 使用 collect(Collectors.toList()) 将流中的元素收集到一个新的列表中。
  3. 方法三:使用 Guava 库

    • 使用 FluentIterable.from(originalList) 创建一个可变的迭代器。
    • 使用 transform 方法将每个 Map 转换为 JSONObject
    • 使用 toList() 方法将结果收集到一个新的列表中。
相关推荐
virus594532 分钟前
悟空CRM mybatis-3.5.3-mapper.dtd错误解决方案
java·开发语言·mybatis
没差c2 小时前
springboot集成flyway
java·spring boot·后端
时艰.2 小时前
Java 并发编程之 CAS 与 Atomic 原子操作类
java·开发语言
编程彩机2 小时前
互联网大厂Java面试:从Java SE到大数据场景的技术深度解析
java·大数据·spring boot·面试·spark·java se·互联网大厂
笨蛋不要掉眼泪2 小时前
Spring Boot集成LangChain4j:与大模型对话的极速入门
java·人工智能·后端·spring·langchain
Yvonne爱编码3 小时前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python
像少年啦飞驰点、4 小时前
零基础入门 Spring Boot:从“Hello World”到可上线微服务的完整学习指南
java·spring boot·微服务·编程入门·后端开发
眼眸流转4 小时前
Java代码变更影响分析(一)
java·开发语言
Yvonne爱编码4 小时前
JAVA数据结构 DAY4-ArrayList
java·开发语言·数据结构
阿猿收手吧!4 小时前
【C++】C++原子操作:compare_exchange_weak详解
java·jvm·c++