Program to convert List of String to List of Integer in Java
The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.
- Using Java 8 Stream API: A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Java 8 Stream API can be used to convert List
to List .Algorithm:
- Get the list of String.
- Convert List of String to Stream of String. This is done using List.stream().
- Convert Stream of String to Stream of Integer. This is done using Stream.map() and passing Integer.parseInt() method as lambda expression.
- Collect Stream of Integer into List of Integer. This is done using Collectors.toList().
- Return/Print the list of String.
Program:
// Java Program to convert// List<String> to List<Integer> in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.*;classGFG {// Generic function to convert List of// String to List of Integerpublicstatic<T, U> List<U>convertStringListToIntList(List<T> listOfString,Function<T, U> function){returnlistOfString.stream().map(function).collect(Collectors.toList());}publicstaticvoidmain(String args[]){// Create a list of StringList<String> listOfString = Arrays.asList("1","2","3","4","5");// Print the list of StringSystem.out.println("List of String: "+ listOfString);// Convert List of String to list of IntegerList<Integer> listOfInteger = convertStringListToIntList(listOfString,Integer::parseInt);// Print the list of IntegerSystem.out.println("List of Integer: "+ listOfInteger);}}chevron_rightfilter_noneOutput:List of String: [1, 2, 3, 4, 5] List of Integer: [1, 2, 3, 4, 5]
- Using Guava’s List.transform():
Algorithm:
- Get the list of String.
- Convert List of String to List of Integer using Lists.transform(). This is done using passing Integer.parseInt() method as lambda expression for transformation.
- Return/Print the list of String.
Program:
// Java Program to convert// List<String> to List<Integer> in Java 8importcom.google.common.base.Function;importcom.google.common.collect.Lists;importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert List of// String to List of Integerpublicstatic<T, U> List<U>convertStringListToIntList(List<T> listOfString,Function<T, U> function){returnLists.transform(listOfString, function);}publicstaticvoidmain(String args[]){// Create a list of StringList<String> listOfString = Arrays.asList("1","2","3","4","5");// Print the list of StringSystem.out.println("List of String: "+ listOfString);// Convert List of String to list of IntegerList<Integer>listOfInteger = convertStringListToIntList(listOfString,Integer::parseInt);// Print the list of IntegerSystem.out.println("List of Integer: "+ listOfInteger);}}chevron_rightfilter_noneOutput:List of String: [1, 2, 3, 4, 5] List of Integer: [1, 2, 3, 4, 5]
- Program to convert List of Integer to List of String in Java
- Program to convert set of String to set of Integer in Java
- Program to Convert Set of Integer to Set of String in Java
- Program to Convert List to Map in Java
- Program to Convert Set to List in Java
- Convert a String to a List of Characters in Java
- Convert List of Characters to String in Java
- Convert a List of String to a comma separated String in Java
- Program to Convert List to Stream in Java
- Program to convert Array to List in Java
- Program to Convert a Vector to List in Java
- Convert String into comma separated List in Java
- Program to Convert Set of Integer to Array of Integer in Java
- Convert List to Set in Java
- Convert an Iterator to a List in Java
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



