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()));
4
  • 1
    This is not a duplicate. The problem in this code is wrong lambda syntax. Commented Feb 3, 2015 at 14:50
  • 2
    @LutzHorn a missing parenthesis. Then, it's a typo and should be closed as well. Commented Feb 3, 2015 at 14:51
  • @LuiggiMendoza OK, but for a different reason. Commented Feb 3, 2015 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. Commented Feb 2, 2018 at 11:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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
Are you meant that i need to use collect(Collectors.toList() at the end of expression
The signature of the sorting method is Stream#sorted(Comparator<T>)---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.
The stream doesn't know or care that its source happens to be a list. The Collector will create a list of its own.
List<Train> sorted = airport.stream().sorted(comparing(Train::getName).collect(Collectors.toList());
|
4

Use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.