π Background Information
The Java Stream API gives developers the power to use functional programming techniques on problems that were usually solved with classes in Java. This has a variety of benefits including potential performance gains, the ability to parallelize code, and the ability to model your code as a series of data transformations.
π― Problem Statement
- Write a set of four functions that calculate the minimum, maximum, sum, and average of an array of integers. These functions should use a
forloop like we have seen in the class many times before. - Write a set of four functions that calculate the minimum, maximum, sum, and average of an array of integers. These functions should use the Java Stream API.
β Acceptance Criteria
- I expect to see eight functions total.
- Test your functions thoroughly through JUnit tests.
π Dev Notes
- You are allowed to use any and all helper functions included in the Stream API.
π₯οΈ Example Output
You might define your eight functions like so:
public int maximumUsingForLoop(int[] nums) { ... }
public int minimumUsingForLoop(int[] nums) { ... }
public int sumUsingForLoop(int[] nums) { ... }
public int averageUsingForLoop(int[] nums) { ... }
public int maximumUsingStream(int[] nums) { ... }
public int minimumUsingStream(int[] nums) { ... }
public int sumUsingStream(int[] nums) { ... }
public int averageUsingStream(int[] nums) { ... }π Thought Provoking Questions
- Which syntax do you prefer for these calculations - streams or loops? Why?
- What is the difference between a βmutableβ and βimmutableβ structure in Java?
- What does the
mapfunction do in a stream? - What does the
filterfunction do in a stream? - What does the
reducefunction do in a stream?
πΌ Optional Extra Credit
(1/4 Point) Evens Only
Write a function that takes an array of integers and filters out any odd numbers. You must use the Java Stream API for this solution and test it using JUnit.
(1/4 Point) Odds Only
Write a function that takes an array of integers and filters out any even numbers. You must use the Java Stream API for this solution and test it using JUnit.
(1/4 Point) Add Five
Write a function that takes an array of integers and returns a new array of integers where all of the numbers have been incremented by five. You must use the Java Stream API for this solution and test it using JUnit.
(1/4 Point) Square Numbers
Write a function that takes an array of integers and returns a new array of integers where all of the numbers have been squared. You must use the Java Stream API for this solution and test it using JUnit.
π Useful Links
π Works Cited
N/A