Home > database >  Java : Is it possible to replace map values with different ones each iteration?
Java : Is it possible to replace map values with different ones each iteration?

Time:03-18

I'm trying to update the map returned by getValues() with the value corresponding to the output of calculate(key, nbIter, lastIterValues), but lastIterValues and getValues() are not updating. (I'm not really used to code with Java) Can you please help?

This is the code I wrote:

public void resolve()
    {
        int nbIter = 1;
        
        Map<Integer, Double> lastIterValues = new HashMap<Integer, Double>();
        
        lastIterValues = getValues();
        

        for(int i=0; i < getAF().size(); i  )
        {
            getValues().put(i, 1.0);
        }
        
        lastIterValues = getValues();

        while(nbIter < 10)
        {
            //Doesn't work
            //for (Map.Entry<Integer, Double> entry : getValues().entrySet()) 
            //{
            //    entry.setValue(calculate(entry.getKey(), nbIter, lastIterValues)) ; 
            //}
        
            final int iter = nbIter;
            final Map<Integer, Double> lastIterValues2 = new HashMap<Integer, Double>(getValues());
            
            getValues().replaceAll((k, v) -> calculate(k, iter, lastIterValues2));
            lastIterValues.replaceAll((k, v) -> getValues().get(k));
            
            Iter  = 1;

        }
    }

CodePudding user response:

You can try this for Java 8 by overriding true value sources:

private Map<Integer, Double> getValues() {
    return Map.of(1, 1.0, 2, 2.0, 3, 3.0);
}

public void resolve() {
     getValues().entrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                                          e -> calculate(e.getValue())))
                .forEach((k, v) -> System.out.println("key: "   k   ", val: "   v));
}

private Double calculate(Double value) {
    return value == 1.0 ? 1.1 : value;
}

The initial value of the map was:

key: 1, val: 1.0
key: 2, val: 2.0
key: 3, val: 3.0

The calculated value of the map would be:

key: 1, val: 1.1
key: 2, val: 2.0
key: 3, val: 3.0

good coding! ¯_(ツ)_/¯

CodePudding user response:

You are trying to assign a value to function. That is not allowed in java.

getValues().put(i,1.0);

If you are using a getter than use a variable to store the getter name than call it using . e.g

Object obj = new Object; obj.getValue

Since you already have a variable you are using to store the values than use it until the end and replace the values of getValues using a setter

public void resolve()
    {
        int nbIter = 1;
        
        Map<Integer, Double> lastIterValues = new HashMap<Integer, Double>();
        
        lastIterValues = getValues();
        

        for(int i=0; i < getAF().size(); i  )
        {
            lastIterValues.put(i, 1.0);
        }
       
        //I'm Not sure what you are trying to do here but it should be something like this
        while(nbIter < 10)
        {
        
            final int iter = nbIter;
            final Map<Integer, Double> lastIterValues2 = new HashMap<Integer, Double>(getValues());

            //You should use lastIterValues to calculate not getValues()
            lastIterValues.replaceAll((k, v) -> calculate(k, iter, lastIterValues2));
            
            //This variable isn't defined I think you meant nbIter
            Iter  = 1;

        }
    }
  • Related