3

I want to convert them to ArrayList and I will store them. After that, I have to convert them into old values. Do you have any suggestions? Thanks in advance.

public List<List<String>> phones = new ArrayList<>();
public List<List<Restaurant.Menu>> menus = new ArrayList<>(); 

public ArrayList<String> phones = ?
public ArrayList<String> menus = ?
3
  • are you using java-8? Commented Dec 31, 2017 at 10:52
  • so you need to flat the list? Commented Dec 31, 2017 at 10:52
  • I am using android. Java can help for that. Any solution can help :) I want to convert List<List to ArrayList after that, I will convert Arraylist to List<List. Commented Dec 31, 2017 at 11:01

4 Answers 4

3

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It can work but it gives api level problem in android.
Thank you for first part. Can you convert ArrayList to List<List ?
List<List<String>> menusAccumulator = new ArrayList<>(); for (List<Restaurant.Menu> temp : menus) { List<String> tempAccumulator = new ArrayList<>(); for(Restaurant.Menu m : temp){ tempAccumulator.add(m.toString()); // or m.getName(); } menusAccumulator.add(tempAccumulator); } ?
Sorry, there is a problem in this code. I want to convert menusAccumulator to List<List<Restaurant.Menu>> menus in this time;
0

If you're using Java 8, flatMap can be useful here:

ArrayList<String> phoneList = phones.stream()
                                        .flatMap(List::stream)
                                        .collect(Collectors.toCollection(ArrayList::new));

Comments

0

I am proving my resolution as below said, so you can change as per your own -

    List<String> nonvegList = new ArrayList<String>();
    nonvegList.add("Mutton Keema");
    nonvegList.add("Chicken Keema");
    nonvegList.add("Korma Veg Keema");
    nonvegList.add("Pulaav Biryaani");
    nonvegList.add("Mutton Biryaani");
    nonvegList.add("Chicken Biryaani");

    List<List<String>> menuList = new ArrayList<List<String>>();
    menuList.add(nonvegList);

    ArrayList<String> resultList = new ArrayList<String>(menuList.get(0));
    System.out.println(resultList);

sweet, simple and in understadable format, compatible after Java 6+,

hope this will help you, thanks.

4 Comments

I guess you create new ArrayList for each list(row). If list is too long, it will be problem.
see, you already have a list, and I have just given the 7 lines of code for your understanding right, I can also write these kind of things in single line, so here you have many menu list like vegList, nonvegList, iceCreamList, snacksList etc. added to your menuList (List<List<String>>) and you can easily declare an arrayList object and put the values collection by the time of initialization.
Sorry, can you edit your post and can you add code? My mind confused a bit. How can I get all values in once.
Here before converting the list collection to the class ArrayList, JVM first initializes the ArrayList<String> from the existing List<String> object in the ArrayList<String> constructor part, and thus initializing the resultList variable and this is just possible because the Collection is a super class for both type List and ArrayList, I think I have explained, if found helpful mark this as answer.
0

I used gson to solve it. I share a sample.

Gson gson = new Gson();
ArrayList<String> gsonString = new ArrayList<>();
for(int i=0; i<restaurants.size(); i++)
        gsonString.add(gson.toJson(restaurants.get(i)));
// Store it
tinydb.putListString("tinyRestaurant",gsonString);

And convert again

Gson gson = new Gson();
for(int i=0; i<tinydb.getListString("tinyRestaurant").size(); i++)
            restaurants.add(gson.fromJson(tinydb.getListString("tinyRestaurant").get(i), Restaurant.class));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.