类转换

集合转数组

toArray(T[] array),大小list.size

1. 字符串有整型的相互转换

String a = String.valueOf(2); //integer to numeric string int i = Integer.parseInt(a); //numeric string to an int

2. 把 Java util.Date 转成 sql.Date

java.util.Date utilDate = new java.util.Date(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());


作者:Java工程师-10
链接:https://zhuanlan.zhihu.com/p/27855397
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

3. 把 Array 转换成 Map

import java.util.Map;  
import org.apache.commons.lang.ArrayUtils; public class Main { public static void main(String[] args) { String[][] countries = { { "United States", "New York" }, { "United Kingdom", "London" }, { "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris" } }; Map countryCapitals = ArrayUtils.toMap(countries); System.out.println("Capital of Japan is " + countryCapitals.get("Japan")); System.out.println("Capital of France is " + countryCapitals.get("France")); } }
4.string和byte[]
byte b[] = s.getBytes();//String转换为byte[] 

String t = new String(b);//bytep[]转换为String

5. 把inputStream和string
InputStream   in_nocode   =   new   ByteArrayInputStream(str.getBytes());   
InputStream   in_withcode   =   new   ByteArrayInputStream(str.getBytes("UTF-8"));  
  StringBuffer   out   =   new   StringBuffer(); 
        byte[]   b   =   new   byte[4096]; 
        for   (int   n;   (n   =   in.read(b))   !=   -1;)   { 
                out.append(new   String(b,   0,   n)); 
        } 
        return   out.toString(); 

6,int 和 long

long   a = 10;     int b = (int)a;   

二.将Long型转换为int 型的,这里的Long型是包装类型:

Long a = 10; int b=a.intValue();

三.将int型转化为long型,这里的int型是基础类型:

int a = 10;long b = (int)a;

四.将Integer型转化为long型,这里的Integer型是包装类型:

int a = 10;Long b = a.longValue();

 

转载于:https://www.cnblogs.com/vhyc/p/7684475.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值