Christopher Rucinski
Christopher Rucinski

Reputation: 4867

Reusing the same value inside of a Java Stream

I am trying to use Java Streams to imitate an equation step-by-step. I have a plugin that will allow me to see the results at each step in the debugger and that is my end goal. I would also like to test what modifying specific numbers would do to the output

However, I need to reuse one value that is used at the 1st mapping function for the last mapping function, and I cannot figure out how to do it. I have commented out what I would like to do, but the compiler finds it incorrect.

Basically, I am testing the following mathematical function...

((2x + 10) / 2) - x = 5

List<Month> months = new ArrayList<>(12);

    months.add(new Month(1, "January"));
    months.add(new Month(2, "February"));
    months.add(new Month(3, "March"));
    months.add(new Month(4, "April"));
    months.add(new Month(5, "May"));
    months.add(new Month(6, "June"));
    months.add(new Month(7, "July"));
    months.add(new Month(8, "August"));
    months.add(new Month(9, "September"));
    months.add(new Month(10, "October"));
    months.add(new Month(11, "November"));
    months.add(new Month(12, "December"));

    months.stream()                                        //  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12
            .map((month) -> month.number * 2)              //  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24
            .map((product) -> product + 10)                // 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34
            .map((sum) -> sum / 2)                         //  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17
     // --> .map((quotient) -> quotient - month.number)    //  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5
            .toArray();

Just to be clear, I am trying to reuse the month.number value.

Edit Just to be clear, month.number represents x with the first .map(), I am showing the results of 2x (month.number * 2).

Also, here is the code for Month

private class Month {

    int number;
    String name;

    public Month(int month, String name) {

        this.number = month;
        this.name = name;

    }
}

Upvotes: 0

Views: 191

Answers (2)

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13803

If you need to access variables from previous map()s, it means you separated map() calls unnecessarily. You can do everything in a single one:

 months.stream()                                
   .map(m -> ((m.number * 2) + 10)/2 - m.number)             
   .toArray(Integer[]::new);

or step by step:

months.stream()                                     
  .map(m -> {
      int product = m.number * 2;
      int sum = product + 10;
      int quotient = sum / 2;
      int result = quotient - m.number;
      return result;
  }).toArray(Integer[]::new);

Upvotes: 0

albert_nil
albert_nil

Reputation: 1658

You don't need several maps one after the other for the example you posted. Just do one map that does all that calculations (and you will have then the original month value).

If you anyway want to do it this way, then you will need that your maps do not return the number alone, but instead a pair of month/tmp_value.

There is not a java class specifically aimed to store pairs, but you can use the Map.Entry (AbstractMap.SimpleEntry) or, better, implement your own Pair class.

Upvotes: 2

Related Questions