问题背景
因为在项目常用一些强制转换,有时候忘了,记录一些常用的
注意事项:
//常用的强制转换
int num = 100;
String str = "50";
Integer intNum = Integer.valueOf(num);
Integer intStr = Integer.valueOf(str);
System.out.println(intNum);
System.out.println(intStr);
强制转为long, 低类型可以直接向高类型转换,int可以之间转为long, 不需要强转
long longNum = (long) num;
long longStr = Long.parseLong(str);
System.out.println(longNum);
System.out.println(longStr);
强制转为byte, 转为低类型,必须强制转换, 编译器可以把对象直接转为基本数据类型
byte byteNum = (byte) num;
byte byteStr = Byte.parseByte(str);
System.out.println(byteNum);
System.out.println(byteStr);
HashMap强制转为JSONObject
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("a", "b");
JSONObject jsonObject = new JSONObject();
jsonObject.putAll(hashMap);
System.out.println(jsonObject);
JSONObject强制转为HashMap
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("a1", "b1");
HashMap<String, Object> hashMap1 = new HashMap<>(jsonObject1);
System.out.println(hashMap1);
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);
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);
List强制转为JSONArray
List<Object> list = new ArrayList<>();
list.add("a");
JSONArray jsonArray = new JSONArray();
jsonArray.addAll(list);
System.out.println(jsonArray);
JSONArray强制转为List
JSONArray jsonArray1 = new JSONArray();
jsonArray1.add("a1");
List<Object> list1 = new ArrayList<>(jsonArray1);
System.out.println(list1);
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);
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依赖
<?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 ...