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


Groups > comp.lang.java.programmer > #29592 > unrolled thread

Lambda Expressions - Java 8 Feature

Started byim.manoj.tripathi@gmail.com
First post2014-08-08 22:34 -0700
Last post2014-08-10 14:36 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#29592 — Lambda Expressions - Java 8 Feature

Fromim.manoj.tripathi@gmail.com
Date2014-08-08 22:34 -0700
SubjectLambda Expressions - Java 8 Feature
Message-ID<71d13b39-96dd-4553-b899-7ea808cec0e3@googlegroups.com>
http://www.groupkt.com/post/088a8dea/lambda-expressions---java-8-feature.htm

[toc] | [next] | [standalone]


#29596

FromJan Burse <janburse@fastmail.fm>
Date2014-08-09 13:26 +0200
Message-ID<ls50h3$2pc$1@news.albasani.net>
In reply to#29592
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?

[toc] | [prev] | [next] | [standalone]


#29620

FromSebastian <news@seyweiler.dyndns.org>
Date2014-08-10 14:36 +0200
Message-ID<ls7ovo$b05$1@news.albasani.net>
In reply to#29596
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


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web