Reputation: 33
How can I [flat] merge multiple arrays into a single List<String>
?
For example:
private String title[] = { "Cup Cake", "Donut", "Eclair", "Froyo",
"Ginger Bread", "Honey Comb", "Icecream Sandwich", "Jelly Bean" };
private String desc[] = { "version: 1.5", "version: 1.6",
"version: 2.0 & 2.1", "version: 2.2", "version: 2.3",
"version: 3.0", "version: 4.0", "version: 4.1" };
private int thumb[] = {3,4,7,8,10,11,15,16};
Upvotes: 0
Views: 9281
Reputation: 412
Source:
private String title[] = { "Cup Cake", "Donut", "Eclair", "Froyo",
"Ginger Bread", "Honey Comb", "Icecream Sandwich", "Jelly Bean" };
private String desc[] = { "version: 1.5", "version: 1.6",
"version: 2.0 & 2.1", "version: 2.2", "version: 2.3",
"version: 3.0", "version: 4.0", "version: 4.1" };`
private int thumb[] = {3,4,7,8,10,11,15,16};
Assuming you want to have a list of Strings:
final List<String> original = Stream.of(title, desc).collect(Collectors.toList());
original.addAll(Stream.of(thumb).map(String::valueOf).collect(Collectors.toList()));
Upvotes: 1
Reputation: 1134
Assuming that you wish to add the array of Integers into the same List of Strings:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class StringArrayTest
{
public static void main(String[] args)
{
String title[] = { "Cup Cake", "Donut", "Eclair", "Froyo",
"Ginger Bread", "Honey Comb", "Icecream Sandwich", "Jelly Bean" };
String desc[] = { "version: 1.5", "version: 1.6",
"version: 2.0 & 2.1", "version: 2.2", "version: 2.3",
"version: 3.0", "version: 4.0", "version: 4.1" };
int thumb[] = {3,4,7,8,10,11,15,16};
List<String> nameList = new ArrayList(Arrays.asList(title));
nameList.addAll(Arrays.asList(desc));
nameList.add(Arrays.toString(thumb)); //adds thumb as a single string entry
}
}
Upvotes: 2
Reputation:
Try this
List<Object> list =
Stream.of(Stream.of(title), Stream.of(desc), IntStream.of(thumb).mapToObj(i -> i))
.flatMap(s -> s)
.collect(Collectors.toList());
System.out.println(list);
Upvotes: 6
Reputation: 1968
All your elements need to be of the same type, for example String.
You can try something like:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class StringArrayTest
{
public static void main(String[] args)
{
String title[] = { "Cup Cake", "Donut", "Eclair", "Froyo",
"Ginger Bread", "Honey Comb", "Icecream Sandwich", "Jelly Bean" };
String desc[] = { "version: 1.5", "version: 1.6",
"version: 2.0 & 2.1", "version: 2.2", "version: 2.3",
"version: 3.0", "version: 4.0", "version: 4.1" };
List<String> newList = new ArrayList<String>();
newList.addAll( Arrays.asList(title));
newList.addAll(Arrays.asList(desc));
for (String e : newList)
{
System.out.println(e);
}
}
}
Upvotes: 0
Reputation: 310
You will need to iterate over each of them. Also, each element in the list will need to be the same type (unless you want them all to be Object. try:
list List<String> = new ArrayList<String>
// First array
for (String s: title) {
list.add(s);
}
// Do the same for other arrays
Alternatively
list List<Object> = new ArrayList<Object>
// First array
for (Object s: title) {
list.add(s);
}
In the second case, the list is just less useful since you would have to end up manually type checking everthing before calling any methods on the elements.
Upvotes: 0