Convert list <String> to list <Integer> directly
After parsing my file s contains AttributeGet:1,16,10106,10111
So I need to get all decimal places in attributeIDGet list. I know that there are several ways to do this. But is there a way to certainly convert List<String> to List<Integer> . Since the code below complains about type mismatch, so I tried to execute Integer.parseInt, but it doesn't seem to 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(",")); } No, you need to iterate over the array
for(String s : strList) intList.add(Integer.valueOf(s)); Using Java8:
stringList.stream().map(Integer::parseInt).collect(Collectors.toList()); Guava converters do the trick.
import com.google.common.base.Splitter; import com.google.common.primitives.Longs; final Iterable<Long> longIds = Longs.stringConverter().convertAll( Splitter.on(',').trimResults().omitEmptyStrings() .splitToList("1,2,3")); Using lambda:
strList.stream().map(org.apache.commons.lang3.math.NumberUtils::toInt).collect(Collectors.toList());
No, you have to iterate over each element:
for(String number : numbers) { numberList.add(Integer.parseInt(number)); } The reason for this is that there is no easy way to convert a list of one type to any other type. Some transformations are impossible or must be performed in a certain way. In fact, the transformation depends on the objects involved and the context of the transformation, so there is no solution "one size fits all." For example, what if you have a Car object and a Person object. You cannot convert a List<Car> to a List<Person> directly, as that really doesn't make sense.
If you use the Google Guava library , this is what you can do, see # transform Lists
String s = "AttributeGet:1,16,10106,10111"; List<Integer> attributeIDGet = new ArrayList<Integer>(); if(s.contains("AttributeGet:")) { List<String> attributeIDGetS = Arrays.asList(s.split(":")[1].split(",")); attributeIDGet = Lists.transform(attributeIDGetS, new Function<String, Integer>() { public Integer apply(String e) { return Integer.parseInt(e); }; }); } Yes, agree with the answer above that it is bloated but stylish. But this is another way.
Why don't you use a stream to convert a list of strings to a list of integers? as below
List<String> stringList = new ArrayList<String>(Arrays.asList("10", "30", "40", "50", "60", "70")); List<Integer> integerList = stringList.stream() .map(Integer::valueOf).collect(Collectors.toList()); the complete operation may be something like this
String s = "AttributeGet:1,16,10106,10111"; List<Integer> integerList = (s.startsWith("AttributeGet:")) ? Arrays.asList(s.replace("AttributeGet:", "").split(",")) .stream().map(Integer::valueOf).collect(Collectors.toList()) : new ArrayList<Integer>(); If you are allowed to use lambdas from Java 8, you can use the following code example.
final String text = "1:2:3:4:5"; final List<Integer> list = Arrays.asList(text.split(":")).stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList()); System.out.println(list); Use of external libraries. Plain old new Java!
No, there is no way (what I know) to do this in Java.
Basically you will have to convert each entry from String to Integer.
What you are looking for can be achieved in a more functional language, where you can pass the conversion function and apply it to all elements of the list ... but this is not possible (this will apply to every element in the list).
Overkill:
However, you can use the function from Google Guava (http://docs.guava-libraries.googlecode.com/ git / javadoc / com / google / common / base / Function.html) to simulate a more functional approach, if that is what are you looking for.
If you worry about iterating over the list twice, instead of splitting, use the Tokenizer and convert each integer token to Integer before adding to the list.
You can use Java 8's Lambda functions to achieve this without a loop
String string = "1, 2, 3, 4"; List<Integer> list = Arrays.asList(string.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList()); Here is another example to show the power of Guava. Although this is not the way I write code, I wanted to put it all together to show what functional programming Java provides Java.
Function<String, Integer> strToInt=new Function<String, Integer>() { public Integer apply(String e) { return Integer.parseInt(e); } }; String s = "AttributeGet:1,16,10106,10111"; List<Integer> attributeIDGet =(s.contains("AttributeGet:"))? FluentIterable .from(Iterables.skip(Splitter.on(CharMatcher.anyOf(";,")).split(s)), 1)) .transform(strToInt) .toImmutableList(): new ArrayList<Integer>();