Java 8 lambda expression for list/array conversion


1). Convert List to List ( List of Strings to List of Integers)

List<Integer>  integerList = stringList.stream().map(Integer::parseInt).collect(Collectors.toList()); 


// the longer full lambda version: 
List<Integer>  integerList  = stringList.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());


2). Convert List to int[](List of Strings to int array)

int[] intArray = stringList.stream().mapToInt(Integer::parseInt).toArray();


3). Convert String[] to List ( String array to List of Integers)

List<Integer>  integerList = Stream.of(array).map(Integer::parseInt).collect(Collectors.toList());


4). Convert String[] to int[] (String array to int array)

int[] intArray = Stream.of(stringArray).mapToInt(Integer::parseInt).toArray();

5). Convert String[] to List (String array to Double List)

List<Double> doubleList = Stream.of(stringArray).map(Double::parseDouble).collect(Collectors.toList());

6). Convert int[] to String[] (int array to String array)

String[] stringArray = Arrays.stream(intArray).mapToObj(Integer::toString).toArray(String[]::new);

7).  Convert 2D int[][] to List> ( 2D int array to nested Integer List)

List<Integer>  list = Arrays.stream(dataSet).map(Arrays::asList).collect(Collectors.toList());



Post a Comment

Popular posts from this blog

Yield Price Sri Lanka - Android Application

Manage SOAPAction of the Out Message

How to clean Registry log (REG_LOG) table