Pavel
Pavel

Reputation: 1288

Java LIst default and modified list

I get a list from the database ingredients, and I make a new list somelist = ingredinets. I convert some values of somelist with convert(somelist). This is changing both lists. Is there a way to keep the original list ingredients? Probably, I am doing something wrong. Should I get a second list from the database?

    List <Ingredient> somelist = new ArrayList<>();
    somelist = ingredients;

    convert.showImperial(somelist);


public List<Ingredient> showImperial(List<Ingredient> ingredients) {


    for (int i = 0; i < ingredients.size(); i++) {
        switch (ingredients.get(i).getMeasurement()) {
            case GRAMS: {
                Double value = ingredients.get(i).getValue();

                if (value >= 453.59237) {
                    value = value / 453.59237;
                    BigDecimal rounded = new BigDecimal(value);
                    rounded=rounded.setScale(2, RoundingMode.HALF_DOWN);
                    ingredients.get(i).setValue((rounded.doubleValue())); //to pound
                    ingredients.get(i).setMeasurement(POUNDS);
                } else {
                    value = value * 16 / 453.59237; //to oz;
                    System.out.println(value);
                    BigDecimal rounded = new BigDecimal(value);
                    rounded = rounded.setScale(1, RoundingMode.HALF_DOWN);
                //    System.out.println(rounded);
                 //   System.out.println(rounded.doubleValue());
                    ingredients.get(i).setValue(rounded.doubleValue());
                    ingredients.get(i).setMeasurement(OUNCE);
                }
                break;
            }
            case ML: {
                double value = ingredients.get(i).getValue();
                ingredients.get(i).setValue(value / 240);
                ingredients.get(i).setMeasurement(CUP); //to cup
                break;
            }
            default: {
                break;
            }
        }
    }
    return ingredients;
}

Upvotes: 1

Views: 127

Answers (3)

nachokk
nachokk

Reputation: 14413

If you are using an ArrayList as an implementation in somelist you can just create using this constructor.

   List <Ingredient> somelist = new ArrayList<>(ingredients);

Note that this is a copy of List, if you add some elements into ingredients won't be reflected in someList and viceversa.

Example:

List <Ingredient> somelist = new ArrayList<>(ingredients);
 ingredients.add(new Ingredient()); // won't be reflected in somelist

But if you mutate elements from the list for example via setters then it will reflected in both list.This is a shallow copy.

List <Ingredient> somelist = new ArrayList<>(ingredients);
Ingredient aux=ingredients.get(0); 
aux.setSomeProperty("someProperty");//will be reflected in both list

To make a deep copy you have to add a clone method for your Ingredient class to make a deep copy.

Upvotes: 0

PixelArtDragon
PixelArtDragon

Reputation: 224

What you're doing is setting the second list to be a reference to the first. What you want to do is somehow pass the information from the first to the second. Otherwise when one is changed, so is the other.

Upvotes: 0

Mark Elliot
Mark Elliot

Reputation: 77084

This line:

somelist = ingredients;

Is not making a copy of ingredients, it's setting somelist to be the same as ingredients.

You can simply add all the elements from ingredients to somelist:

somelist.addAll(ingredients);

...but you also appear to be modifying those, and a List holds a reference to the elements, not the actual elements. So you'll need to go a step further and copy one level deeper:

for (Ingredient ingr : ingredients) {
    somelist.add(new Ingredient(ingr)); // or use whatever constructor is appropriate.
}

Upvotes: 1

Related Questions