Transform a for in lambda with variable interaction

1

How do I transform the following into a Lambda code?

The idea is to multiply maxScore by i so that with each interaction it raises the number, only for the first 5 results.

    for(int i=0;i<5;i++){
        list.get(i).setScore(maxScore + (0.01d * i));
    }

The idea of the code is more or less this:

    List<HashObject> list = createObjects(10);
    Double maxScore = 0.9d;

    for(int i=0;i<5;i++){
        list.get(i).setScore(maxScore + (0.01d * i));
    }

HashObject class:

    public class HashObject {
        private Integer id;
        private String name;
        private Double score;

        /* Getters and Setters */
    }
    
asked by anonymous 04.08.2017 / 15:17

2 answers

1

Sirs,

I solved the problem with the command:

IntStream.range(0,5).forEach(p -> list.get(p).setScore(0.01d * (double)p));

As the IntStream.range (0.5) command loop, forEach looped through, and p is the interaction of the loop (the i in the case of for).

    
04.08.2017 / 22:26
0

Now, you get the elements of your list and apply a forEach:

Example:

  List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);

  list.forEach(n -> System.out.println(n));

Anything looks at this link: link

    
04.08.2017 / 16:12