Object Oriented Architectures
and Secure Development
Lecture 1 – Recap
Objects and classes
Fields (properties, state)
Methods (behaviour)
o Getters
o Setters
o toString / CompareTo / equals + hashCode / …
Constructors
o Copy constructor
o Constructor chaining
Objects in collections
List
o Classic
o Stack
o Queue
Set
o Equals / hashCode
Map
o Equals / hashCode
Other topics
Exceptions
o Try
o Throw
o Catch
Inheritance and interfaces
o Extends / implements
o Overriding methods
Static
o Methods and fields
o Of the class, not of instances
Naming conventions
Capital letter + camel casing (all types: classes, interfaces, enums)
Small letter + camel casing (all methods, fields, variables, …)
Capital Letters, snake casing (static final fields (constants), enum instance names (not studied))
1 Object Oriented Architectures and Secure Development 2022-2023
,Lecture 2 – Stream API
Java.util.stream
Contains various classes to support functional-style operations on streams of elements. Nothing
to do with file I/O.
o Map
o Reduce
o Filter
o Foreach
o …
But also lots more…
What is stream?
Represents a sequence of elements.
supports various types of operations, allowing for computations on said elements.
Example 1: creating a stream from a list of values
Stream.of("Jughead", "Betty", "Archie", "Veronica")
.map(String::toLowerCase)
.sorted()
.forEach(System.out::println);
1. Stream is mapped to another stream
2. We provide the method to apply to each element as argument
1. The :: operator is also called method reference operator
2. Pass a reference to methods – in this case – map() and forEach() methods
3. Is shorthand for a lambda expression that executes just one method, comparable to:
Stream.of("Jughead", "Betty", "Archie", "Veronica")
.map(e -> e.toLowerCase())
.sorted()
.forEach(e -> System.out.println(e));
2 Object Oriented Architectures and Secure Development 2022-2023
,Example 2: converting a collection into a stream
List<Product> products = new ArrayList<>();
products.add(new Product(1, "cookies", 2.99));
products.add(new Product(2, "chocolate", 3.99));
products.add(new Product(3, "bananas", 1.99));
products.stream()
.filter(e -> e.getPrice() > 2)
.sorted(Comparator.comparingDouble(Product::getPrice))
.forEach(System.out::println);
Intermediate versus terminal operations
Intermediate operations
Returns a stream
We can apply chaining – method1().method2().method3();
Examples:
o .map()
o .filter()
o .sorted()
o .peek()
Stream.of("Jughead", "Betty", "Archie", "Veronica")
.map(String::toLowerCase)
.sorted()
.forEach(System.out::println);
Terminal operations
Returns nothing (void) or non-stream result (int, double, …)
Therefore, after a terminal operations, no longer possible to chain
Examples:
o .forEach()
o .sum()
o .max()
Stream.of("Jughead", "Betty", "Archie", "Veronica")
.map(String::toLowerCase)
.sorted()
.forEach(System.out::println);
Creating streams
Stream.of(obj1, obj2, obj3, …)
IntStream.of(int1, int2, int3, …)
DoubleStream.of(dbl1, dbl2, dbl3, …)
LongStream.of(lng1, lng2, lng3, …)
Collection.stream()
3 Object Oriented Architectures and Secure Development 2022-2023
, Transforming object streams to primitive streams
double avg = products.stream()
.mapToDouble(e -> e.getPrice())
.average()
.orElse(0);
System.out.println(String.format("Average price is %.2f", avg));
Average() returns a OptionalDouble
Here, calling orElse(0) will return the double inside or 0 if there is none.
Other useful method of OpttionalXXX: isPresent(Boolean)
Transforming primitive streams to object streams
IntStream.range(1, 4)
.mapToObj(e -> new Product(e, "Prod " + e, e*2))
.forEach(System.out::println);
Collect
Very useful terminal operation
Allows you to transform elements of a stream into a different result, such as List, Set or Map
List
List<Person> persons = new ArrayList<>();
persons.add(new Person("Jughead", 18));
persons.add(new Person("Betty", 18));
persons.add(new Person("Veronica", 21));
persons.add(new Person("Archie", 20));
List<Person> filteredPersons =
persons.stream()
.filter(p -> p.getAge() > 18)
.collect(Collectors.toList());
Map
Map<Integer, List<Person>> personsPerAge =
persons.stream()
.collect(Collectors.groupingBy(Person::getAge));
personsPerAge.forEach((a, p) -> System.out.println(
String.format("%s %s", a, p)));
Average
double avgAge =
persons.stream()
.collect(Collectors.averagingDouble(Person::getAge));
Summarize
DoubleSummaryStatistics stats =
persons.stream()
.collect(Collectors.summarizingDouble(Person::getAge));
4 Object Oriented Architectures and Secure Development 2022-2023