Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)

导读:本篇文章讲解 Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)

问题背景

因为在项目常用一些强制转换,有时候忘了,记录一些常用的
注意事项:

强制转换

  1. 强制转为Integer, 同类型编译器可以把对象直接转为基本数据类型
        //常用的强制转换
        int num = 100;
        String str = "50";

        Integer intNum = Integer.valueOf(num);
        Integer intStr = Integer.valueOf(str);
        System.out.println(intNum);
        System.out.println(intStr);
  1. 强制转为long, 低类型可以直接向高类型转换,int可以之间转为long, 不需要强转
        long longNum = (long) num;
        long longStr = Long.parseLong(str);
        System.out.println(longNum);
        System.out.println(longStr);
  1. 强制转为byte, 转为低类型,必须强制转换, 编译器可以把对象直接转为基本数据类型
        byte byteNum = (byte) num;
        byte byteStr = Byte.parseByte(str);
        System.out.println(byteNum);
        System.out.println(byteStr);
  1. HashMap强制转为JSONObject
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("a", "b");
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(hashMap);
        System.out.println(jsonObject);
  1. JSONObject强制转为HashMap
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("a1", "b1");
        HashMap<String, Object> hashMap1 = new HashMap<>(jsonObject1);
        System.out.println(hashMap1);
  1. Object的HashMap强制转为JSONObject
        HashMap<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put("a2", "b2");
        Object mapTemp = hashMap2;
        if (mapTemp instanceof Map) {
            JSONObject jsonObject2 = new JSONObject();
            HashMap<String, Object> temp = (HashMap) mapTemp;
            jsonObject2.putAll(temp);
            System.out.println(jsonObject2);
        }
  1. Object的JSONObject强制转为HashMap
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("a3", "b3");
        Object jsonTemp = jsonObject3;
        if (jsonTemp instanceof Map) {
            HashMap<String, Object> map2 = new HashMap<>((Map) jsonTemp);
            System.out.println(map2);
        }
  1. List强制转为JSONArray
        List<Object> list = new ArrayList<>();
        list.add("a");
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(list);
        System.out.println(jsonArray);
  1. JSONArray强制转为List
        JSONArray jsonArray1 = new JSONArray();
        jsonArray1.add("a1");
        List<Object> list1 = new ArrayList<>(jsonArray1);
        System.out.println(list1);
  1. Object的List强制转为JSONArray
        List<Object> list2 = new ArrayList<>();
        list2.add("a2");
        Object listObj = list2;
        if(listObj instanceof List) {
            JSONArray jsonArray2 = new JSONArray();
            jsonArray2.addAll((List)listObj);
            System.out.println(jsonArray2);
        }
  1. Object的JSONArray强制转为List
        JSONArray jsonArray3 = new JSONArray();
        jsonArray3.add("a3");
        Object arry3 = jsonArray3;
        if(arry3 instanceof List) {
            List<Object> list3 = new ArrayList<>((List)arry3);
            System.out.println(list3);
        }

12 List类型通过stream连接成String类型

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
String str = list.stream().map(Object::toString).collect(Collectors.joining(","));

项目搭建

1 引入pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yg</groupId>
    <artifactId>forceConvert</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>forceConvert</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2 测试类

package com.yg.forceconvert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * @Author suolong
 * @Date 2022/6/1 21:36
 * @Version 2.0
 */
public class main {

    public static void main(String[] args) {
        //常用的强制转换
        int num = 100;
        String str = "50";

        //1. 强制转为Integer, 同类型编译器可以把对象直接转为基本数据类型
        Integer intNum = Integer.valueOf(num);
        Integer intStr = Integer.valueOf(str);
        System.out.println(intNum);
        System.out.println(intStr);

        //2. 强制转为long, 低类型可以直接向高类型转换,int可以之间转为long, 不需要强转
        long longNum = (long) num;
        long longStr = Long.parseLong(str);
        System.out.println(longNum);
        System.out.println(longStr);

        //3. 强制转为byte, 转为低类型,必须强制转换, 编译器可以把对象直接转为基本数据类型
        byte byteNum = (byte) num;
        byte byteStr = Byte.parseByte(str);
        System.out.println(byteNum);
        System.out.println(byteStr);

        //4. HashMap强制转为JSONObject
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("a", "b");
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(hashMap);
        System.out.println(jsonObject);


        //5. JSONObject强制转为HashMap
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("a1", "b1");
        HashMap<String, Object> hashMap1 = new HashMap<>(jsonObject1);
        System.out.println(hashMap1);


        //6. Object的HashMap强制转为JSONObject
        HashMap<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put("a2", "b2");
        Object mapTemp = hashMap2;
        if (mapTemp instanceof Map) {
            JSONObject jsonObject2 = new JSONObject();
            HashMap<String, Object> temp = (HashMap) mapTemp;
            jsonObject2.putAll(temp);
            System.out.println(jsonObject2);
        }


        //7. Object的JSONObject强制转为HashMap
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("a3", "b3");
        Object jsonTemp = jsonObject3;
        if (jsonTemp instanceof Map) {
            HashMap<String, Object> map2 = new HashMap<>((Map) jsonTemp);
            System.out.println(map2);
        }

        //8. List强制转为JSONArray
        List<Object> list = new ArrayList<>();
        list.add("a");
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(list);
        System.out.println(jsonArray);


        //9. JSONArray强制转为List
        JSONArray jsonArray1 = new JSONArray();
        jsonArray1.add("a1");
        List<Object> list1 = new ArrayList<>(jsonArray1);
        System.out.println(list1);


        //10. Object的List强制转为JSONArray
        List<Object> list2 = new ArrayList<>();
        list2.add("a2");
        Object listObj = list2;
        if(listObj instanceof List) {
            JSONArray jsonArray2 = new JSONArray();
            jsonArray2.addAll((List)listObj);
            System.out.println(jsonArray2);
        }


        //11. Object的JSONArray强制转为List
        JSONArray jsonArray3 = new JSONArray();
        jsonArray3.add("a3");
        Object arry3 = jsonArray3;
        if(arry3 instanceof List) {
            List<Object> list3 = new ArrayList<>((List)arry3);
            System.out.println(list3);
        }

    }
}

总结

可以快速使用强制转换

作为程序员第 147 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)

Lyric: 无力的躺在干枯的河

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110728.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!