Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Objects First with Java , Summaries of Computer Science

Summary of Objects First with Java

Typology: Summaries

2023/2024

Available from 05/06/2024

US-Summery
US-Summery 🇮🇹

4.2

(15)

937 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
lOMoARcPSD|26858109
D. J. Barnes, M. Kolling,
Objects First with Java
pf3
pf4
pf5

Partial preview of the text

Download Objects First with Java and more Summaries Computer Science in PDF only on Docsity!

lOMoARcPSD|

D. J. Barnes, M. Kolling,

Objects First with Java

Reading VII: Functional Processing of Collections

QUIZ:

October 22

Processing Recall^ ~^ Java^ is^ an^ imperative^ object-oriented^ language.

(Textbook Ch. V)

Functional languages are another class of language that use a different style of programming. Streams and lambdas (also known as closures) were added with Java 8 in 2014. These new elements make Java now somewhat of a hybrid language. A collection object can store an arbitrary number of other objects. Lamdas or anonymous functions are segments of code that is passed as a parameter to a method, stored, and executed later by a method. Observe the pseudo-code below: (Sighting record) - > { System.out.println(record.getDetails()); }

  • Notice there is no public or private keyword; it is assumed to be public.
  • No return type is specified because it can be worked out by the compiler from the final statement in the lambda’s body. In the above example, the println method is void, so the lambda’s return type is also void.
  • No name is given to the lambda; it starts with the parameter list.
  • The arrow is the distinctive notation for a lambda that separates the parameter list from the body.
  • No associated class means that it has no instance fields or constructors. forEach() method refers to the idea of ‘do this for each element.’
  • The parameter for this method will be a lambda.
  • Example: this method uses a forEach loop with a lambda parameter. The lambda itself also has a parameter, record. Every element of the sightings list will be used as a parameter to this lambda. The details of every element will be printed. public void printList() { for(Sighting record:sightings) { System.out.println(record.getDetails()); } }

Processing

(Textbook Ch. V)

  • Reducing a stream collapses all of the elements into a single result. This can be done by adding all of the elements, finding the minimum, etc.
  • A pipeline is the combination of two or more stream functions in a chain, where each function is applied in turn (example: filter all of the elements to only elephants, map each sighting to find the amount of elephants, and find the sum with the reduce function.
  • Example: pipelines start with a source (a stream) followed by a sequence of operations— intermediate operations (produce a new stream to apply the next operation) and terminal operations (produces a result). sightings.filter(spotter == spotterID) .filter(period == dayID) .reduce(count elements); Stream Functions in Detail The filter method creates a subset of the original stream. The subset will typically be elements that fulfill some condition. If the condition is met, the element will be processed in some way. The filter method receives a lambda that receives an element and returns a Boolean result.
  • Lambdas that take an element and return true or false are called predicates.
  • Example: A stream is first created from the sightings list to be applied to the filter function. Remember, parameters do NOT need to be specified. sightings.stream() .filter(s – “Elephant”.equals(s.getAnimal())) . ... //do something
  • Example: Each operation creates a fresh stream (the input stream is left unmodified). public void printSightingsOf(String animal) { sightings.stream() .filter(s - > animal.equals(s.getAnimal())) .forEach(s - > System.out.println (s.getDetails())); } The map method is an intermediate operation that creates a stream with different objects.
  • Example: This code segments would result in a stream of integers which could then be processed further. public void printSightingsBy(int spotter) { sightings.stream() .filter(sighting - > sighting.getSpotter() == spotter) .map(sighting - > sighting.getDetails()) .forEach(details - > System.out.println (details)); }

Processing

(Textbook Ch. V)

  • Example: This code segments would result in a stream of integers which could then be processed further. sightings.stream() .filter(sighting - > sighting.getSpotter() == spotter) .map(sighting - > sighting.getCount()) . ... //do something The reduce method is a terminal operation that “collapses” its input stream to a single object or value.
  • Example: Notice that the reduce method has TWO parameters (below observe the 01 , the identity or starting balue, and the lambda). The lambda itself also has two parameters: total and count. public int getCount(String animal) { return sightings.stream() .filter(sighting - > animal.equals(sighting. getAnimal())) .map(sighting - > sighting.getCount()) .reduce(0, (total, count) - > return total + count); }
  • Example: Counting in a traditional manner. This is done by 1) declaring a variable to hold the final value, 2) iterating over the list to determine which records are needed, 3) obtaining the count (mapping), 4) adding the count, and 5) returning the sum. Notice how the variable total acts as an accumulator. public int getCount(String animal) { int total = 0; for(Sighting sighting : sightings) { if(animals.equals(sighting.getAnimal())) total = total + sighting.getCount(); } return total; } Removing from a Collection
  • Example: Remove all of the sightings with a count of zero. Notice that is a method of the collection (and not of a stream), so it does not motify the original collection. public void removeZeroCounts() { sightings.removeIf(sighting - > sighting.getCount() == 0); } Other Stream Methods
  • count, findFirst, max (reduce operations); limit and skip (filter). 1 The value to which the running total is initialized.