• Generics, Collections

    Written by Jonas Altrock (ew20b126@technikum-wien.at).


    The fifth exercise is about generic classes and the java collection framework.

    A generic class (or interface) has a type parameter <T>, which is passed in when the class is used. The parameter can be everywhere in the definition of the class where a concrete type would be used. So as the type for properties, for constructor or method arguments, as the return type for methods, and as a type for variables declared in the class scope or any method body.

    The java collection framework contains interfaces and classes pertaining to an abstract treatment of "multiple things" as a collection, i.e. arrays, lists, trees, sets, and more. The aim is to be able to treat many data structures in the same way, making it possible to switch out one implementation for a different one that might be faster in some circumstances, without having to change the code that does the things with the data contained within a collection.

    The official java documentation has more information about the Java collections framework.

    This assignment was a lot of work:

    • We have to implement classes implementing Comparator<Track>:
      • DurationComparator
      • TitleComparator
      • WriterComparator
    • Implement classes extending the abstract and generic MyMatcher<T>:
      • DurationMatcher
      • TitleMatcher
    • Extend the formatters with header and top separator, by implementing the MyFormatter<T> interface:
      • CSVTrackFormatter
      • ShortTrackFormatter
    • And implement the class MyTrackContainer, which contains a set of tracks and allows selecting, filtering and sorting them, using the matchers and comparators.
      MyTrackContainer

    The entity classes remain the same as in ES04, as does the ConsoleScanable interface and implementation.