Convert array of Strings to list of Integers?

William Toscano

This is not a duplicate question because I'm specifically asking to convert an ARRAY of STRINGS to a LIST of INTEGERS. In other words, converting both different types of lists AND different object types at the same time.

import java.util.*;
import java.util.stream.Collectors;

String[] allAnswers = {"2", "4", "1"}

int[] allAnswersInts = Arrays.stream(allAnswers).mapToInt(Integer::parseInt).toArray();

List<Integer> allAnswerList = Arrays.stream(allAnswersInts).boxed().collect(Collectors.toList());

Is there a faster or more practical way to do this?

Andreas

You only need to stream once.

Instead of using int Integer::parseInt(String s), you should use Integer Integer::valueOf(String s), so you don't have to call boxed() or rely on auto-boxing.

Then use collect(Collectors.toList()) directly, instead of creating intermediate array first.

List<Integer> allAnswerList = Arrays.stream(allAnswers)    // stream of String
                                    .map(Integer::valueOf) // stream of Integer
                                    .collect(Collectors.toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert a list of strings containing list-of-integers into an array

List of strings to array of integers

Convert list of strings into list of integers

convert a list of strings list into integers

Convert a list of strings to a list of integers

Convert a list of integers to list of strings

Convert array of strings to an array of integers

Convert list of strings into tuple of integers

R: convert strings in array into integers

Convert a list of strings of lists to a list of integers

convert list of strings from file to list of integers

How to convert list of strings to list of integers?

Convert array of strings in arrays of integers in a dataframe column

Convert the Strings and Integers of a NumPy Array into Floats

Convert array of Strings to Array list

Get List of Integers from array of Strings

Sorting Array List containing strings and integers

Convert array of strings to List<string>

Pandas convert column where every cell is list of strings to list of integers

In PyParsing, how to define a setParseAction function to convert a list of strings to a list of integers?

Trying to use list comprehension to convert list of strings to integers?

Lambda expression to convert array/List of String to array/List of Integers

Pandas - Convert strings in a list to integers using a dictionary and sum values

How to convert a list of strings representing tuples to tuples of integers?

Convert integers inside a list into strings and then a date in python 3.x

How to convert nested list strings to integers then sort them in python 3?

How do I convert a list of strings to integers in Python

searching a list of strings for integers

How to convert a list of strings into a numeric numpy array?