Java (parallel) streams
Use the Java tutorial on Aggregate Operations as a reference if you are unsure.
- Given a list of names like (william, jones, aaron, bob, frank, gillian), calculate using streams the sum of the letters of all names longer than 4 letters.
- Calculate the sum of all even numbers from 2 to a given number N using streams. Ideally, do not manually generate the list of numbers.
- Given a class Person as below, print the age of the oldest person in a list of Persons using streams.
- Using the same class, print the name of the oldest person in a list of Persons using streams.
class Person { private String name; private int age; private String nationality; public Person(String name, int age, String nationality) { this.name = name; this.age = age; this.nationality = nationality; } public Person(String name, int age) { this(name, age, ""); } public String getName() { return name; } public int getAge() { return age; } public String getNationality() { return nationality; } }
Parallel streams doing real work
Most of this is from lab 3, the description is repeated for convenience.
We want to find the number of divisors of the number in a range of positive numbers which has the maximum number of divisors. A divisor is a number which divides another number with remainder 0. For example, 9 has two divisors, namely 3 and 1, whereas 8 has three, namely 1,2, and 4 (if the 1 is included or not does not matter for the maximum). Using the int data type should be enough for this exercise.
As a first step, write a program which finds the divisors for a number.
Then extend this to find the maximum number of divisors for a range in a sequential fashion. Try this out for eg the range from 1 to 1000 and 1 to 100,000 and 1 to 10,000,000. Record how long it takes for 1 to 10,000,000.
Finally, implement a concurrent version using parallel streams. How much of an speedup do we have?