Logo

Programming-Idioms

  • Rust
  • Python
  • Java

Idiom #258 Convert list of strings to list of integers

Convert the string values from list a into a list of integers b.

import static java.util.Arrays.stream;
b = stream(s)
    .mapToInt(Integer::parseInt)
    .toArray();
b = a.stream()
    .map(Integer::parseInt)
    .toList();
import static java.lang.Integer.parseInt;
for (String s : a) b.add(parseInt(s));
let b: Vec<i64> = a.iter().map(|x| x.parse::<i64>().unwrap()).collect();

a has type Vec<&str>
let b: Vec<i32> = a.iter().flat_map(|s| s.parse().ok()).collect();

ok converts a Result to an Option. flat_map collects all values of Some.
b = [int(elem) for elem in a]
b = [*map(int, a)]
System.Linq;
var b = a.Select(i => int.Parse(i)).ToList();

New implementation...