Archives: Type Conversion

Java – Convert String to Long

[code lang=”java”]public class StringToLong {

public static void main(String args[]) {
String strLong = "2323232";

//Convert String to long using Long.valueOf(),better because
//it can cache frequently used long values
long number = Long.valueOf(strLong);
System.out.println("String to long conversion using valueOf :" + number);

//Convert String to long in Java using java.lang.Long object
strLong ="4444444444444444444";
Long numberObject = new Long(strLong);
number = numberObject; //auto boxing will take care of long to Long conversion
System.out.println("String to long conversion example using Long object: " + number);

//Convert String to long with Long.parseLong method in Java
strLong="999999999999999999";
Long parsedLong = Long.parseLong(strLong) ;
number = parsedLong; //auto-boxing will convert Long object to primitive long
System.out.println("String to long conversion using parseLong() method: " + number);

//Convert String to long using Long.decode() method in Java
strLong = "-1023454";
number = Long.decode(strLong);
System.out.println("String to long conversion using decode() method: " + number);

//String to long in hexadecimal format
strLong = "0xFFFF";
number = Long.decode(strLong);
System.out.println("String to long example in hex format decode() method: " + number);

//String to long in octal format
strLong = "0777";
number = Long.decode(strLong);
System.out.println("String to long example in octal format decode() method: " + number);
}
}[/code]
Output:

String to long conversion using valueOf :2323232
String to long conversion example using Long object: 4444444444444444444
String to long conversion using parseLong() method: 999999999999999999
String to long conversion using decode() method: -1023454
String to long example in hex format decode() method: 65535
String to long example in octal format decode() method: 511


Java – Convert Array to Map using Commons ArrayUtils

This example use the Apache Commons lang’s ArrayUtils class to convert a two dimensional array to a Map object.

[code lang=”java”]import java.util.Map;
import org.apache.commons.lang.ArrayUtils;

public class ArrayToMapExample {

public static void main(String[] args) {
//
// A two dimensional array of countries capital.
//
String[][] countries = {{"United States", "New York"},
{"United Kingdom", "London"},
{"Netherlands", "Amsterdam"},
{"Japan", "Tokyo"},
{"France", "Paris"}};

//
// To convert an array to a Map each array elements must
// be an array with at least two elements where the first
// element will be the key and the second element will be
// the value.
//
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"));
}

}[/code]


Java – Convert Array to List

[code lang=”java”]import java.util.*;
import java.io.*;

public class ArrayToCollection {

public static void main(String args[]) throws IOException {

BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("How many elements you want to add to the array: ");
int n = Integer.parseInt(in.readLine());
String[] name = new String[n];
for(int i = 0; i < n; i++) {
name[i] = in.readLine();
}
List list = Arrays.asList(name);
System.out.println();
for(String li: list) {
String str = li;
System.out.print(str + " ");
}

}

}[/code]
Output:

How many elements you want to add to the array:
red white green

red white green