Java 8 lambda use cases
Converting int[] to List of Integer
Using Arrays.stream()
we can convert like below
public static void main(String ... str){
int input[] = {2,3,4,4,5,5,5,6,6,6,6,6,9,9,10,10,11,11,11,11,11,11,11,11,15};
List<Integer> print = Arrays.stream(input).boxed().collect(Collectors.toList());
}
Converting String to Character array
Using String chars()
method and mapToObj
, we can convert String to char array like below
public static void main(String ... str){
String input= "somestring";
Character[] inAsCharArray = input.chars().mapToObj(item -> (char)item).toArray(Character[]::new);
}
Converting String to List of Integer
Using String chars()
we can convert each chars to its ASCII equivalent.
public static void main (String ... str){
String romanStr = "IVXLD";
List<Integer> charlist= romanStr.chars().boxed().collect(Collectors.toList());
}