Institute of Computer Science
  1. Courses
  2. 2022/23 fall
  3. Concurrent Programming Languages (LTAT.06.022)
ET
Log in

Concurrent Programming Languages 2022/23 fall

  • Pealeht
  • Loengud
  • Labs
  • Viited
  • Homework

Java (parallel) streams

Use the Java tutorial on Aggregate Operations as a reference if you are unsure.

  1. 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.
  2. 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.
  3. Given a class Person as below, print the age of the oldest person in a list of Persons using streams.
  4. 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?

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment