# Numerical streams example

I am reading "Java 8 In Action" (by Raoul-Gabriel Urma, Mario Fusco and Alan Mycroft), section 5.6.3, pages 116 and 117. The code that is presented handles calculating so-called "Pythagorean Triples". Page 116 shows the first attempt, and page 117 shows an improved attempt to generate these triples, where both use the .rangeClosed() method.

I have found some optimizations that go beyond the book and I would like to share them here. I did some simple System.currentTimeMillis() calculations to see if my modifications were improvements, and they appeared to be marginally better than that found in the book. Can you provide better improvements, explanations or metrics for this code?

public void test() {

long time1 = System.currentTimeMillis();

/*
* From text, page 116
*/
IntStream.rangeClosed(1,  100).boxed()
.flatMap(a -> IntStream.rangeClosed(a, 100)
.filter(b -> Math.sqrt(a*a + b*b) % 1 == 0)
.mapToObj(b -> new int[]{a, b, (int)Math.sqrt(a*a + b*b)})
)
.forEach(c -> System.out.println("["+c+" "+c+" "+c+"]"));

long time2 = System.currentTimeMillis();

System.out.println();

long time3 = System.currentTimeMillis();

/*
* From text, page 117, I added "map(...)" so that end result are ints, not doubles
*/
IntStream.rangeClosed(1, 100).boxed()
.flatMap(a -> IntStream.rangeClosed(a, 100)
.mapToObj(b -> new double[]{a, b, Math.sqrt(a*a + b*b)})
.filter(t -> t % 1 == 0)
.map(b -> new int[]{(int)b, (int)b, (int)b})
)
.forEach(c -> System.out.println("["+c+" "+c+" "+c+"]"));

long time4 = System.currentTimeMillis();

System.out.println();

long time5 = System.currentTimeMillis();

/*
* My optimization attempt #1: now mapToObj(...) has c%1!=0 conditional, filter checks array element not null
*/
IntStream.rangeClosed(1, 100).boxed()
.flatMap(a -> IntStream.rangeClosed(a, 100)
.mapToObj(b -> {
double c = Math.sqrt(a*a + b*b);
return new Object[]{a, b, c % 1 == 0 ? (int)c : null};
})
.filter(d -> d != null)
.map(e -> new int[]{(int)e, (int)e, (int)e})
)
.forEach(f -> System.out.println("["+f+" "+f+" "+f));

long time6 = System.currentTimeMillis();

System.out.println();

long time7 = System.currentTimeMillis();

/*
* My optimization attempt #2: now mapToObj(...) has c%1!=0 conditional, filter checks "array element" not 0
*/
IntStream.rangeClosed(1, 100).boxed()
.flatMap(a -> IntStream.rangeClosed(a, 100)
.mapToObj(b -> {
double c = Math.sqrt(a*a + b*b);
return new int[]{a, b, c % 1 == 0 ? (int)c : 0 };
})
.filter(t -> t != 0)
)
.forEach(d -> System.out.println("["+d+" "+d+" "+d+"]"));

long time8 = System.currentTimeMillis();

System.out.println();

long time9 = System.currentTimeMillis();

/*
* My optimization attempt #3: now mapToObj(...) has c%1!=0 conditional, filter checks "collection element" not null
*/
IntStream.rangeClosed(1, 100).boxed()
.flatMap(a -> IntStream.rangeClosed(a, 100)
.mapToObj(b -> {
double c = Math.sqrt(a*a + b*b);
return (c % 1 != 0) ? null : new int[]{a, b, (int)c};
})
.filter(t -> t != null)
)
.forEach(d -> System.out.println("["+d+" "+d+" "+d+"]"));

long time10 = System.currentTimeMillis();

System.out.println();

long timeDelta1 = time2 - time1;
long timeDelta2 = time4 - time3;
long timeDelta3 = time6 - time5;
long timeDelta4 = time8 - time7;
long timeDelta5 = time10 - time9;

System.out.println("timeDelta1: " + timeDelta1 + ", timeDelta2: " + timeDelta2 + ", timeDelta3: " + timeDelta3 + ", timeDelta4: " + timeDelta4 + ", timeDelta5: " + timeDelta5);

}

public static void main(String[] args){
ReduceTest reduceTest = new ReduceTest();
reduceTest.test();
}


NOTE: It appears that you can use return in a .forEach() method, but not in a .mapToInt() method. Using return in the lambda passed into the .mapToInt() method would remove the need to have the .filter() method. It seems like that would be an improvement to the streams API.