In mathematics, a square number or perfect square is an integer that is the square of an integer. In other words it is a number multiplied by a number and can be written as 4 x 4. In the snippets below we will show how to square an arraylist of numbers using plain java, java 8 and guava.
Straight up Java
Java 8
First loading an array list with a series of random numbers we will show two different ways approaches that result in the same output. Creating a function that will result in a number multiple by itself, we will convert the an arraylist to a stream and call the map function passing in the function square. Next using the java 8 forEach we will iterate and output the numbers. The second snippet will perform the same operations but collect the values back to a list.
Google Guava
ReactiveX - RXJava
This snippet will show how to square values in a sequence using RxJava. First, we will creating a function using java 8 syntax that accepts one value and then multiplies it by itself. Next, using the Observable.from method will convert the List to an Observable so it will emit the items to the sequence. Calling the map method we will apply the square function to each of the values.