'Replace an object in a List<Object> when Object is a List? Java

Hello again stackoverflow, I have a question concerning List of Objects.

I have tried out writing some things, but can't find it though. How do I find an Object in a list of objects, when these objects are lists of its own?

this is what i have so far:

Recipe is a List of: {modification[], ingredients[], recipeBookName, recipeName}

public void removeFromBook(Recipe recipeName) {
    recipes = getRecipes();
    emptyRecipe = getEmptyPage(recipeBookName);

Now, I want to replace a Recipe which has a recipeName, by the emptyRecipe. I think it will be something like:

for(r in recipes) {
    if(r.recipeName == recipeName) {
        list.replace(Recipe, emptyRecipe)
    } else {

    }
}

any ideas? :)

here's my constructor for the Recipe class:

    public String[] modifications;
public String[] ingredients;
public String recipeName;
public String partOfRecipeBook;

public Recipe(String recipeName, String[] modifications, String[] ingredients, String recipeBookName){
    setRecipeModifications(modifications);
    setRecipeIngredients(ingredients);
    setRecipeName(recipeName);
    setRecipeBookName(recipeBookName);
}


Solution 1:[1]

Your approach looks fine, except that you should compare strings with equals:

if(r.recipeName.equals(recipeName)) {

Now an even easier way would be to store your recipes in a Map:

Map<String, Recipe> recipes = new HashMap<String, Recipe>();
recipes.put("Pizza with Lobster", new Recipe());

when you want to replace a recipe:

recipes.put("Pizza with Lobster", emptyRecipe);

and the old recipe has been replaced.

Solution 2:[2]

 private List<TimeModel> timeModels = new ArrayList<TimeModel>();

then you simply put

  timeModels.set(0, new TimeModel("6:00 PM"));

and after that set yourAdapter.notifyDataSetChanged();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 assylias
Solution 2 procrastinator