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

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

  • 问题背景
  • 强制转换
  • 项目搭建
  • 总结
  • Lyric: 无力的躺在干枯的河

问题背景

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

  • 可以下载源码进行参考

强制转换

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

项目搭建

1 引入pom依赖


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.0version>
        <relativePath/> 
    parent>
    <groupId>com.yggroupId>
    <artifactId>forceConvertartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>forceConvertname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.62version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        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 …

Lyric: 无力的躺在干枯的河

你可能感兴趣的:(java开发常遇问题,java,开发语言,java-ee)