Why You Want Lambda Expressions

There are three main reasons why programmers want lambda expressions:

  • More-concise code
    Lambda expressions provide a neater way of implementing Java
    classes that have only one function. For example, if your code has a large number of anonymous inner classes—defining things such as listeners and handlers in UI code or
    callables and runnables in concurrent code—moving to the lambda expressions style can make your code much shorter and easier to understand.
  • Ability to modify methods by supplying additional functionality
    Sometimes, methods don’t quite have the functionality we want. For example, the contains() method in the Collection interface returns true only if the exact object passed in is present in the collection. There’s no way to tweak that functionality, for example, to have the method return true if the string we’re looking for is present but uses different capitalization. Loosely, what we want to do is “pass in some new code of our own” to an existing method, which will then call the code that we passed in. Lambda expressions can provide a good way to represent the code that has been passed in and should be called back.
  • Better support for multicore processing
    Today’s CPUs have multiple cores. This means that your multithreaded code can truly run in parallel, as opposed to being time-shared on a single CPU. Lambda expressions will help you write simple code that can use these cores efficiently by supporting the use of functional programming idioms in Java. For example, you will be able to manipulate large collections in parallel using all the available hardware threads on your CPU by taking advantage of parallel versions of idioms such as filter, map, and reduce (which we will be meeting shortly).

External Versus Internal Iteration
Up until now, the standard way of dealing with a Java collection is via external iteration. It’s called external iteration because there is control flow, external to the collection,
which is used to iterate over the elements contained in the collection.

The alternative is to require the collection object to manage the iterator (and loop) internally. This approach is called internal iteration, and it is the preferred choice when working with lambda expressions. In addition to new syntax for lambda expressions, Project Lambda includes a major upgrade to the collections libraries. The aim of the collections upgrade is to make it much easier to write code that uses internal iteration to support a range of well-known functional programming idioms.

Ovaj unos je objavljen u Nekategorizirano. Bookmarkirajte stalnu vezu.


Popunite niže tražene podatke ili kliknite na neku od ikona za prijavu:

WordPress.com Logo

Ovaj komentar pišete koristeći vaš WordPress.com račun. Odjava /  Izmijeni )

Google+ photo

Ovaj komentar pišete koristeći vaš Google+ račun. Odjava /  Izmijeni )

Twitter picture

Ovaj komentar pišete koristeći vaš Twitter račun. Odjava /  Izmijeni )

Facebook slika

Ovaj komentar pišete koristeći vaš Facebook račun. Odjava /  Izmijeni )

Spajanje na %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.