1

I was using java 8 new feature 'Lambda expression' its cool feature to use. I think it helps only developer to simplify the coding. Does it have any performance impact on my application.

private void sortCities(List<String> cities){   //Conventional way
  Collections.sort(cities, new Comparator<String>() {
     @Override
     public int compare(String s1, String s2) {
        return s1.compareTo(s2);
     }
  });

}

private void sortCities(List<String> cities){  //Using Lambda Expression
  Collections.sort(cities, (s1, s2) -> s1.compareTo(s2));

}

Holger
  • 285,553
  • 42
  • 434
  • 765
Venkat
  • 147
  • 1
  • 1
  • 10

1 Answers1

1

No, it does not. In your particular case (without capturing arguments) lambda will be actually more performant as the comparator will be created only once and reused, while with anonymous class it will be created each time you call the sortCities method.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334