This is an assignment to the class Programmieren 3 at the University of Applied Sciences Rosenheim.
In this assignment we'll be looking at
- static and regular inner classes
- anonymous classes
- lambda expressions and
@FunctionalInterface
by implementing a basic linked list.
A linked list consists of a controller (the list class) and elements that contain the actual payload (the container or element class).
The controller class maintains a reference to the first list element (head
), and every element points to its successor (or to a null
value, if it's the last).
The following UML shows all classes of this assignment (including the interfaces java.util.Iterator<T>
and java.lang.Iterable<T>
; we'll cover ).
Note: the concept of an iterator will be discussed in detail in a few weeks.
- Create a fork of this repository.
- Clone your fork to get a local working copy.
- Import the project to your IDE (choose Gradle project)
- What is a static class?
- Refresh your knowledge on UML (e.g. here)
- What's the difference between a regular inner and static inner class?
- Can you think of some use cases for both?
- Implement
Element
as static inner class ofSimpleListImpl
.- Why is this class static, and ideally
private
?
- Why is this class static, and ideally
- Implement the
Iterator
interface as inner class ofSimpleListImpl
.- Why is it helpful to make this class non-static?
- Add the
Iterable
interface to yourSimpleListImpl
, and implement the required methods.- Why is implementing the
Iterable
interface essential for a (good) list implementation? (Hint: Check the test cases!) - Are there any language definition constraints to this?
- Why is implementing the
- Implement the
filter
method for yourSimpleListImpl
class (seeSimpleFilter
interface). - Check the given test suite for an example on
- how to use an anonymous class with an interface.
- how an anonymous class can be replaced by a lambda expression.
- Add some test methods and implement another filter logic (e.g. every third number, or any number smaller than a certain value).
- Review anonymous classes and lambdas.
- Lambda expressions look very convenient; can you think of a scenario where they should not be used?
- Recall how scoping works for anonymous (inner or local) classes; can you think of a scenario where to avoid them?