类转换

集合转数组

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