Exercise 06: Lambda Functions
This week’s lab work is intended help you better understand lambda functions.
Pre-lab
Please bring these exercises printed out or written out and completed with you to lab. Doing the pre-lab will save you time during the lab.
P0. If you have a collection called myList, what Java code would you have to write to apply some code to each of the members in the list?
P1. Given the following code:
public void printStudent (Student s)
{
System.out.println (s.getDetails());
}
What is the equivalent lambda in Java? What syntax variations are possible?
P2. Given the animal-monitoring-v1 example, write pseudo-code for determining how many elephants a particular spotter saw on a particular day.
P3. Given the animal-monitoring-v1 example, write pseudo-code to create a stream containing only those sightings that have a count greater than 0.
What To Hand In
Please hand in:
- zip-folder with your AnimalMonitor code
- lab report as pdf
The lab report should have:
- Answers to the questions in the assignment below
- Complete transparency on who did what
- The strategy you chose for working in a team & how it worked out *(OR if you worked alone: ) a reflection on your own working strategy
- A summary of what you did & learned in the lab
- Details on which challenges you encountered, how you tried to solve them & their solution (OR if you did not encounter any challenges: ) a reflection on the exercises themselves
- For practise: How you tested your solutions
Lab assignments are due the day before your next lab at 10pm.
Assignment
Download the animal-monitoring-v1 project and check that it runs. Now rewrite the
printList
method in the AnimalMonitor class to use a lambda, just as we did in the lecture. Now apply each of the variations that you recorded in P1, compile them and run them. Did they all work? Record the results for your lab report.Rewrite the
printSightingsOf
method in the AnimalMonitor class to use streams and lambdas. Test to make sure that your project still works as before.Write a method
printSightingsInPeriod
in theAnimalMonitor
class to print the details of all the sightings recorded within a certain periodperiodID
, which is passed as a parameter to the method.Write a method
printSightingsOfInPeriod
that uses two filter calls to print details of all the sightings of a particular animal made in a particular period — the method takes theanimal
name and theperiodID
as parameters. Does the order of the two filter calls matter in your solution? Justify your answer.Write a method
printCounts
to print the counts of all sightings of a particular animal. Your method should use the map operations as part of the pipeline. If a pipeline contains a filter operation and a map operation, does the order of the operations matter to the final result? Justify your answer.Rewrite the
printEndangered
method in your project to use streams and test that it works correctly. Detail how you tested this method in your report.(For the bored) There is a special “::” notation in Java that can be used with lambdas. Research the syntax, and rewrite printSightingsBy to use this syntax. Does the operation of the method change?
(For the bored) Add a method to AnimalMonitor that takes two parameters, a spotter-ID and a day-ID and returns a String containing the names of the animals seen by the spotter on that particular day. You should include only animals whose sighting count is greater than zero. Now add a method that takes an animal and a day-ID and returns the spotters who saw this animal, if any, on that particular day.