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?
CompletableFutures
Write a small GUI program which allows the user to enter a URL and hit a button. The program will then in the background fetch the HTML code from the URL and display it in the GUI. This should not block the user interface. For fetching the page, supplyAsync is an option. thenAccept could be used to update the GUI. For a GUI, Swing is the standard choice for Java. Hint: Have a look at this for issues to watch out for when updating a swing GUI.
You can find a GUI implementation to work with here.