Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thospfuller/db707b7082d2a1295fc49e8e7db4c031 to your computer and use it in GitHub Desktop.
Save thospfuller/db707b7082d2a1295fc49e8e7db4c031 to your computer and use it in GitHub Desktop.
Example Two: How to turn an array into an ArrayList in Java using the java.util.stream API.
@Grab(group='com.jamonapi', module='jamon', version='2.82')
import com.jamonapi.Monitor
import com.jamonapi.MonitorFactory
import static java.util.Arrays.asList
import java.util.stream.Collectors;
import java.util.List;
import java.util.Arrays;
import java.util.Random
def random = new Random()
def arrayCapacity = 10000
def numbers = new int[arrayCapacity + 1]
for (int ctr in 0..arrayCapacity) {
numbers[ctr] = random.nextInt()
}
def iterations = 100000
def monitor = MonitorFactory.start("Convert an array to an ArrayList performance monitor via the java.util.stream example")
for (ctr in 0..iterations) {
def exampleList =
Arrays
.stream(numbers)
.boxed()
.collect(
Collectors.toList()
) as java.util.List
}
monitor.stop ()
println "iterations: $iterations, performance: $monitor"
return;
@thospfuller
Copy link
Author

@thospfuller
Copy link
Author

thospfuller commented Apr 9, 2024

Note that line #16 means we need to call the boxed method on line 31 yielding performance results that are not limited to the Java Streams API alone. If we change line #16 to use a java.lang.Integer and we remove the call to boxed on line 31 the average time to run this script five times is 8.7 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment