A long example
- Download a Java project.
-
Add a
compareTomethod to theSectionInfoso that it implements theComparableinterface. In the sort, give priority to department, number, and section in that order. Also, invokeCollections.sortto sort the courses before printing them. -
Add a third class
SectionInfoCompareLimitthat implementsComparator<SectionInfo>. This time sort by limit, department, number, and section. Also, use the two-argument version ofCollections.sortto sort the courses by limit this time. -
Make your
SectionInfoCompareLimitclass a nested class ofSectionInfoRun. (I think it must be astaticclass.) This should eliminate a Java file. -
Make your
SectionInfoCompareLimitclass a local class of themainmethod ofSectionInfoRun. This is a JDK 8 thing. -
Make your
SectionInfoCompareLimitclass an anonymous class of themainmethod ofSectionInfoRun. -
Finally, use a lambda expression as your second argument to
Collections.sort. My starts with something like:
(si1, si2) ->
How did they do it?
How do you add functions to a programming language without functions? Download and read Introducing Java 8 from O’Reilly.
Download the following Java classes and run MethodReferencesTest.
The to do some interesting changes.
By the way, doesn’t Oracle also do something with databases?
References to ponder
- λ
- Functional interfaces