Post

Java Conversions

int[] -> Integer[]

1
2
int[] a = {0, 1, 2};
Integer[] b = Arrays.stream(a).boxed().toArray(Integer[]::new);

Integer[] -> int[]

1
2
3
Integer[] a = {0, 1, 2};
int[] b = Arrays.stream(a).mapToInt(Integer::intValue).toArray();
b = Arrays.stream(a).mapToInt(i -> i).toArray();

List<Integer> -> int[]

1
2
int[] b = list.stream().mapToInt(Integer::valueOf).toArray();
b = list.stream().mapToInt(i -> i).toArray();

int[] -> List<Integer>

1
2
int[] a = {0, 1, 2};
List<Integer> b = Arrays.stream(a).boxed().collect(Collectors.toList());

Integer[] -> Set<Integer>

1
2
Integer[] a = {0, 1, 2};
Set<Integer> b = Arrays.stream(a).collect(Collectors.toSet());

int[] -> Iterable<Integer>

1
2
int[] a = {0, 1, 2};
Iterable<Integer> b = IntStream.of(a).boxed().iterator();

int[] -> double[]

1
2
int[] a = {0, 1, 2};
double[] b = IntStream.of(a).mapToDouble(i -> i).toArray();

List -> Array:

1
2
List<String> list = new ArrayList<>();
String[] array = list.toArray(new String[0])

Array -> Stream

public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive)

char + int

1
char c = (char)('a' + i);

char number -> int

1
2
char c = '9';
int a = c - '0';

String -> int

public static int parseInt(String s, int radix) throws NumberFormatException

This post is licensed under CC BY 4.0 by the author.