Converting ListInteger to ListString
As far as I know, iterate and instantiate is the only way to do this. Something like (for others potential help, since I'm sure you know how to do this):
List<Integer> oldList = ...
/* Specify the size of the list up front to prevent resizing. */
List<String> newList = new ArrayList<>(oldList.size());
for (Integer myInt : oldList) {
newList.add(String.valueOf(myInt));
}
Cast Listint to Liststring in .NET 2.0
.NET 2.0 has the ConvertAll
method where you can pass in a converter function:
List<int> l1 = new List<int>(new int[] { 1, 2, 3 } );
List<string> l2 = l1.ConvertAll<string>(delegate(int i) { return i.ToString(); });
Convert all strings in a list to int
Given:
xs = ['1', '2', '3']
Use map
then list
to obtain a list of integers:
list(map(int, xs))
In Python 2, list
was unnecessary since map
returned a list:
map(int, xs)
Convert ListString to ListInteger directly
No, you need to loop over the array
for(String s : strList) intList.add(Integer.valueOf(s));
Java: Convert ListInteger to String
One way would be:
Iterate
over list, add each item to StringBuffer
(or) StringBuilder
and do toString()
at end.
Example:
StringBuilder strbul = new StringBuilder();
Iterator<Integer> iter = list.iterator();
while(iter.hasNext())
{
strbul.append(iter.next());
if(iter.hasNext()){
strbul.append(",");
}
}
strbul.toString();
Convert a list of integers into a comma-separated string
Yes. However, there is no Collectors.joining
for a Stream<Integer>
; you need a Stream<String>
so you should map
before collecting. Something like,
System.out.println(i.stream().map(String::valueOf)
.collect(Collectors.joining(",")));
Which outputs
1,2,3,4,5
Also, you could generate Stream<Integer>
in a number of ways.
System.out.println(
IntStream.range(1, 6).boxed().map(String::valueOf)
.collect(Collectors.joining(","))
);
How to convert Liststring to Listint?
listofIDs.Select(int.Parse).ToList()
Convert ListString to ListInteger for only valid Integer
In order to remove all non-numeric characters from each string, you need to apply map()
before converting the stream of String
into stream of Integer
.
List<Integer> nums =
strList.stream()
.map(s -> s.replaceAll("[^\\d]", ""))
.map(Integer::valueOf)
.collect(Collectors.toList());
System.out.println(nums);
Output for input {"464 mm", "80 mm", "1000", "1220", "48 mm", "1020"}
[464, 80, 1000, 1220, 48, 1020]
Note:
trim()
afterreplaceAll()
is redundant.- Since you are adding all the contents of the list generated by the stream pipeline into another list, you might substitute
collect(Collectors.toList())
withtoList()
which is accessible with Java 16 onwards. The difference is thattoList()
returns an unmodifiable list and it more performant than collector. - Iterative solutions almost always perform better than stream-based. Lambdas and streams were introduced in Java to provide a way of organizing the code that more readable and concise.
Convert array of Strings to list of Integers?
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());
Convert a list of integers to string
There's definitely a slicker way to do this, but here's a very straight forward way:
mystring = ""
for digit in new:
mystring += str(digit)
Related Topics
How to Add an Extra Source Directory for Maven to Compile and Include in the Build Jar
Recursively List All Files Within a Directory Using Nio.File.Directorystream;
Generics:List<? Extends Animal> Is Same as List<Animal>
Apache Httpclient Interim Error: Nohttpresponseexception
How to Identify End of Inputstream in Java
How to Capture a Jframe's Close Button Click Event
Package a Non-Modular Javafx Application
How Is This Private Variable Accessible
Validation of a List of Objects in Spring
Test for Floating Point Equality. (Fe_Floating_Point_Equality)
Difference Between Role and Grantedauthority in Spring Security
Exception Handling:Throw, Throws and Throwable
Should Getters and Setters Be Synchronized
How to Pause and Resume a Thread in Java from Another Thread