For the first scenario, you can flatten the phones nested list into a single list and then collect into an ArrayList.
ArrayList<String> result =
phones.stream()
.flatMap(Collection::stream)
.collect(Collectors.toCollection(ArrayList::new));
For the second scenario, you will need to extract the string representation of the Menu objects given you've overridden toString, otherwise you'll need to extract some type of property from the Menu objects in order to project from Menu to String.
Given you've overridden toString, then do it this way:
ArrayList<String> menuResult =
menus.stream()
.flatMap(Collection::stream)
.map(Menu::toString)
.collect(Collectors.toCollection(ArrayList::new));
Given you need to extract some property from menus, then do it this way:
ArrayList<String> menuResult =
menus.stream()
.flatMap(Collection::stream)
.map(Menu::getName)
.collect(Collectors.toCollection(ArrayList::new));
If your API level doesn't support these features then you can use:
// flatten List<List<String>> to ArrayList<String>
ArrayList<String> phonesAccumulator = new ArrayList<>();
for (List<String> temp : phones) {
phonesAccumulator.addAll(temp);
}
// flatten List<List<Restaurant.Menu>> to ArrayList<String>
ArrayList<String> menusAccumulator = new ArrayList<>();
for (List<Restaurant.Menu> temp : menus) {
for(Restaurant.Menu m : temp){
menusAccumulator.add(m.toString());
// or m.getName();
}
}