3

Can some please help me with syntax of following java code?

list.stream()
.filter(name -> name.startsWith("f"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);`

is method filter(name -> name.startsWith("f") is being called on the result of list.stream() and then .map(String::toUpperCase) is being called on its result ? What version of java is this? Where I can read about this more?

ariefbayu
  • 21,849
  • 12
  • 71
  • 92
MTARIQ
  • 63
  • 4
  • _"is method filter(name -> name.startsWith("f") is being called on the result of list.stream()_" - yes. _"and then .map(String::toUpperCase) is being called on its result ?"_ - yes. _"Where I can read about this more?"_ - The terms you are looking for are _"java streams"_ and _"method references in java"_. – Yousaf Sep 29 '21 at 04:52
  • This is standard method chaining. Lamba expressions are a more recent addition but the rest is nothing new. – shawnt00 Sep 29 '21 at 05:22
  • 1
    The term you are looking for is [Aggregate Operations](https://docs.oracle.com/javase/tutorial/collections/streams/index.html) – the Hutt Sep 29 '21 at 05:42
  • Poor title. Rewrite the summarize your specific technical issue. – Basil Bourque Sep 29 '21 at 06:21

4 Answers4

3

Your code snippet is a Java stream, and consists of a pipeline of several processing steps. Here is an explanation:

list.stream()
    .filter(name -> name.startsWith("f"))   // retain only names starting with 'f'
    .map(String::toUpperCase)               // map the name to uppercase
    .sorted()                               // sort the stream ascending by name
    .forEach(System.out::println);          // print out each name

In plain English, this stream says to take an input list of names, remove any name not starting with 'f', uppercase all names, sort them ascending by name, and finally print out each name to the console.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
3

In answer to your question, the lambda you're seeing (name -> name.startsWith("f")) is a feature added in Java 8. This version of Java also adds method references, which are essentially a quicker way to write many lambdas which merely call an existing method. The line .map(String::toUpperCase) could have been written as .map(name -> name.toUpperCase()). Similarly .forEach(System.out::println) could have been .forEach(name -> System.out.println(name)).

Documentation on Oracle.com for further reading:

Chris
  • 26,361
  • 5
  • 21
  • 42
1

The stream is one of the functional element of Java which uses Lambda Expressions (unnamed function).

You have a list and convert it to a stream.

For each element you will use the filter function which removes the elements that do not safisfy the criteria. This selection criteria is given as a Lambda Function. Its argument is the element and it should return with a boolean value. So the name should start with 'f'.

Then you use the 'map' function to convert the elements of the list into another type using a Lambda Function again. In this case your list contains string and your LambadaFunction is the String::toUpperCase. This a short hand format of (name) -> {return String.toUpperCase(name);}. In other words, the toUpperCase function will be called with each element of the list and the 'map' will produce a new stream which contains the results.

Next you sort the stream.

Finally, Each element of the stream is given as an argument of the 'println' method.

The result should contains a sorted list of names starting with F.

Zsolt Toth
  • 39
  • 2
1
list.stream()
.filter(name -> name.startsWith("f"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);

The whole idea of Java streams is to enable functional-style operations on streams of elements, using Lambda experession and Method reference. Method references are a special type of lambda expressions

In functional-style operations the execution is performed from left to right that means

stream() is called on result of list

filter() is called on result of list.stream()

map() is called on result of list.stream().filter(name -> name.startsWith("f"))

sorted() is called on result of list.stream().filter(name -> name.startsWith("f")).map(String::toUpperCase)

forEach is called on the result of list.stream().filter(name -> name.startsWith("f")).map(String::toUpperCase).sorted()

To explore your knowledge you can read from Oracle Java (version 8 onward) Documents

Specially for the topics Lambda expression and method reference :-

1. Lamda Expression

2. Method Reference

Jimmy
  • 995
  • 9
  • 18