1

I tried to use lambda expressions to sort the list of objects based on name in JDK 1.8. but it shows compilation error:

The left-hand side of an assignment must be a variable

Code:

    Train trains = new Train();
    trains.setName("Yerlin");
    trains.setCode("YYT");
    Train trains2 = new Train();
    trains2 .setName("Delhi");
    trains2 .setCode("DEH");
List<Train > traindetail= new ArrayList<Train >();
    traindetail.add(trains);
    traindetail.add(trains2);
    traindetail.stream().sorted(Train object1 , Train object2) -> object1.getName().compareTo(object2.getName()));
Rithik_Star
  • 651
  • 5
  • 14
  • 39
  • 1
    This is not a duplicate. The problem in this code is wrong lambda syntax. –  Feb 03 '15 at 14:50
  • 2
    @LutzHorn a missing parenthesis. Then, it's a typo and should be closed as well. – Luiggi Mendoza Feb 03 '15 at 14:51
  • @LuiggiMendoza OK, but for a different reason. –  Feb 03 '15 at 14:53
  • `traindetail.sort(Comparator.comparing(Train::getName));` would sort your list in-place. you don't need to seperate `stream`, unless you want to maintain seperate lists. – Diablo Feb 02 '18 at 11:10

2 Answers2

5

You are missing a paren:

airport.stream().sorted(Train object1 , Train object2) -> object1.getName().compareTo(object2.getName()));
                        ^--here

However, you would be advised to use a much more concise syntax:

airport.stream().sorted(Comparator.comparing(Train::getName));

Then there will be no parens to miss. With the Streams API usage we usally apply static imports liberally, so

airport.stream().sorted(comparing(Train::getName));

I see you don't assign the stream object and do not apply any terminal operations, so maybe you should be aware that the expression above has still taken no action on your data. You just declared that the stream will be sorted once you apply a terminal operation.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Could you please elaborate.Because I thought lambda expressions won't need comparable interface and just a above statement will sufficient to sort.If not kindly explain more abt stream objects and terminal operation for the above – Rithik_Star Feb 03 '15 at 17:11
  • Are you meant that i need to use collect(Collectors.toList() at the end of expression – Rithik_Star Feb 03 '15 at 17:23
  • 1
    The signature of the sorting method is `Stream#sorted(Comparator)`---so yes, you do need that interface (there was no mention of `Comparable`, though). And yes, you need to collect to list, or print, or whatever makes sense to your use case. – Marko Topolnik Feb 03 '15 at 18:41
  • Collectors.to list will assign in the existing list reference or it creates a new varibale? – Rithik_Star Feb 04 '15 at 07:29
  • 1
    The stream doesn't know or care that its source happens to be a list. The Collector will create a list of its own. – Marko Topolnik Feb 04 '15 at 09:38
  • I am confused.If possible can you help me that what needs to be included in the above piece of code (which i posted) to return the sorted objects based on name – Rithik_Star Feb 04 '15 at 09:49
  • 1
    `List sorted = airport.stream().sorted(comparing(Train::getName).collect(Collectors.toList());` – Marko Topolnik Feb 04 '15 at 10:05
4

Use

//                      v add ( here
airport.stream().sorted((Train object1 , Train object2) ->
  object1.getName().compareTo(object2.getName()));