Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29620

Re: Lambda Expressions - Java 8 Feature

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!.POSTED!not-for-mail
From Sebastian <news@seyweiler.dyndns.org>
Newsgroups comp.lang.java.programmer
Subject Re: Lambda Expressions - Java 8 Feature
Date Sun, 10 Aug 2014 14:36:08 +0200
Organization albasani.net
Lines 36
Message-ID <ls7ovo$b05$1@news.albasani.net> (permalink)
References <71d13b39-96dd-4553-b899-7ea808cec0e3@googlegroups.com> <ls50h3$2pc$1@news.albasani.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net U3iQIbIThSorwPq9x6lODyhuDZ0ZO3l4xnHcflkovBmZZr55+r4o1AyFIrVDDcIu3h6mksX0GNotjgCLrf9hqC2CDGFDo629SYQiOn1NsMrnyzu4cZX3BbLECYPXaVhM
NNTP-Posting-Date Sun, 10 Aug 2014 12:36:08 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="T16ekAWV9DZQTlm9SJ5GGnLj7I7RteG1arBEYPHVmW2e83B8rO7CGHwOh+RvixTrudjoEjtGaJGiAV7YAbgB3lRvx5xLNg6hTeoJk6wQ42aIbqhzS2ZZyAaerzr9CAou"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
In-Reply-To <ls50h3$2pc$1@news.albasani.net>
Cancel-Lock sha1:vVXxU92Co7Rf5tmLOM7bI/ZUoPc=
Xref csiph.com comp.lang.java.programmer:29620

Show key headers only | View raw


Am 09.08.2014 13:26, schrieb Jan Burse:
> im.manoj.tripathi@gmail.com schrieb:
>> http://www.groupkt.com/post/088a8dea/lambda-expressions---java-8-feature.htm
>>
>>
>
> Solution 4:
>
>      Collections.sort(students,(student1, student2) ->
>        student1.getRollNumber().compareTo(student2.getRollNumber()));
>
> Yes or No?
>
That is the same as the blog's solution 3, except you do not declare an 
additional local variable for the comparator. So, the answer is yes, or 
were you trying to make another point?

However, I find the following solution much better.

Solution 5:

	students.sort(comparing(Student::getRollNumber));

The method comparing() is a new static method in interface Comparator.
The method sort() is a new default method in the List interface.
Student::getRollNumber is a method reference, it provides convenient 
shorthand for the corresponding lambda s -> s.getRollNumber()

This coding, apart from being both shorter and more readable than 
solutions 3 or 4, isolates the sort key in the call to comparing, rather 
than extracting it in the comparison code itself.

-- Sebastian


Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Lambda Expressions - Java 8 Feature im.manoj.tripathi@gmail.com - 2014-08-08 22:34 -0700
  Re: Lambda Expressions - Java 8 Feature Jan Burse <janburse@fastmail.fm> - 2014-08-09 13:26 +0200
    Re: Lambda Expressions - Java 8 Feature Sebastian <news@seyweiler.dyndns.org> - 2014-08-10 14:36 +0200

csiph-web