Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<String> to List<Integer> directly

After parsing my file " s" contains AttributeGet:1,16,10106,10111

So I need to get all the numbers after colon in the attributeIDGet List. I know there are several ways to do it. But is there any way we can Directly convert List<String> to List<Integer>. As the below code complains about Type mismatch, so I tried to do the Integer.parseInt, but I guess this will not work for List. Here s is String.

private static List<Integer> attributeIDGet = new ArrayList<Integer>();  if(s.contains("AttributeGet:")) {     attributeIDGet = Arrays.asList(s.split(":")[1].split(",")); } 
like image 653
AKIWEB Avatar asked May 22 '12 17:05

AKIWEB


People also ask

How do I convert a list of strings to a list of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);

How do you convert a string to an integer in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .


1 Answers

Using Java8:

stringList.stream().map(Integer::parseInt).collect(Collectors.toList()); 
like image 131
Dawid Stępień Avatar answered Sep 19 '22 06:09

Dawid Stępień