2

I'm trying to take a list of numbers, filter out the even numbers and then square those even numbers so that the even numbers in the original list are squared. Here's my code:

ArrayList<Long> nums2 = new ArrayList<Long>();
for(long i = 0; i < 100000; i++) 
    nums2.add(i);
nums2.stream().filter(p -> p%2 == 0).forEach(p -> p = (long)Math.pow(p, 2));

Tried a couple other things, but this is where I'm at so far

nosid
  • 48,932
  • 13
  • 112
  • 139
PenguinProgrammer
  • 141
  • 2
  • 5
  • 11
  • Your logic seems fine. Now you need to retrieve the results in another collection. – Luiggi Mendoza May 07 '14 at 14:34
  • Trying to make it effect the same collection – PenguinProgrammer May 07 '14 at 14:37
  • @LuiggiMendoza Just curious, would `p -> p = (long)Math.pow(p, 2)` have no effect because the variable reference would be passed by value so reassigning wouldn't have an effect? I don't have much experience with streams, so I very well might be wrong on this. I'm imagining the lambda to be roughly equivalent to `void method(Long p) { p = (long)Math.pow(p, 2);}`. Is that right? – awksp May 07 '14 at 14:38
  • @user3580294 I only said the logic seems fine. I never said anything about the methods. Anyway, review here: http://stackoverflow.com/q/14830313/1065197 (can't provide an answer before testing it). – Luiggi Mendoza May 07 '14 at 14:40
  • @LuiggiMendoza Sorry, thought you might know. – awksp May 07 '14 at 14:42

4 Answers4

6

You need to do something with the result, for example store it in an array. Also note that you don't need the initial list and can start with a stream directly:

long[] result = IntStream.range(0, 100000) //instead of your initial list
    .filter(p -> p % 2 == 0)               //filters
    .mapToLong(p -> (long) Math.pow(p, 2)) //"replaces" each number with its square
    .toArray();                            //collect the results in an array

Or you could decide to print the result:

IntStream.range(0, 100000)
    .filter(p -> p % 2 == 0)
    .mapToLong(p -> (long) Math.pow(p, 2))
    .forEach(System.out::println);
assylias
  • 321,522
  • 82
  • 660
  • 783
3

An alternative solution would be mapping the results and collecting them into a List using Collectors#toList:

List<Long> results = nums2.stream()
    .filter(p -> p%2 == 0)
    .map(p -> p = (long)Math.pow(p, 2))
    .collect(Collectors.toList());
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

Normally java 8 lambdas are used as function without side-effects (not changing the original list), therefor I suggest that you just the classic for-loops instead when you want to have in-place changes:

    ArrayList<Long> nums2 = new ArrayList<Long>();
    for(long i = 0; i < 100000; ++i){ 
        nums2.add(i);
    }
    long even=0; 
    for(int n=0;n<nums2.size();++n){
        if((even=nums2.get(n))%2==0)
            nums2.set(n, even*even);
    }
0

Using the function and predicate you will easily get the output List ls = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9,-12);

    List<Double> collect3 = ls.stream().filter(ele ->ele%2==0).map(ele -> Math.pow(ele, 2)).collect(Collectors.toList());
    System.out.println("Square of even  elements =" + collect3);   

O/p- Square of even elements =[4.0, 16.0, 36.0, 64.0, 144.0]