Android/Java ArrayList maps to ArrayList… here is a solution to the problem.
Android/Java ArrayList maps to ArrayList
I don’t know how to map an array from a storage type to another type.
Unfortunately I can’t use Java 8.
I have ArrayList<
T> I want to map it so I will have ArrayList<String>
How can I achieve it?
Hypothesis.
T obj has the method obj.toString().
I googled it. All I can find is how to convert ArrayList<String> to String[]
or Map<K, V> to ArrayList
<V>
Thanks for your help!
Solution
The
streaming solution that maps from a certain type T
to String
is as follows:
ArrayList<String> resultSet =
someList.stream()
.map(Object::toString) // or ClassName::toString
.collect(Collectors.toCollection(ArrayList::new));
Equivalent to:
ArrayList<String> accumulator = new ArrayList<>();
for(T obj : someList)
accumulator.add(obj.toString());