DEV Community

lou
lou

Posted on • Updated on

Lambda exemple Comparator bDarija

Had l'exemple fih implementation dial comparator bLambda

hna baghin nretbo list bcomparator walakin had lmerra b lambda

la liste qbl:

 List<Apple> list= Arrays.asList(
                new Apple(140,Color.RED),
                new Apple(190,Color.RED),
                new Apple(150,Color.GREEN),
                new Apple(100,Color.GREEN),
                new Apple(170,Color.RED),
                new Apple(110,Color.GREEN)
                );
Enter fullscreen mode Exit fullscreen mode

rappel code b classe anonyme:

list.sort(new Comparator<Apple>() {
@Override
public int compare(Apple a1, Apple a2) {
    return a1.getWeight()-a2.getWeight();

}}
);
Enter fullscreen mode Exit fullscreen mode

code b lambda:

list.sort((Apple a1, Apple a2)-> a1.getWeight()-a2.getWeight());

Enter fullscreen mode Exit fullscreen mode

Resultat la liste mn b3d:

Apple [weight=100, color=GREEN]
Apple [weight=110, color=GREEN]
Apple [weight=140, color=RED]
Apple [weight=150, color=GREEN]
Apple [weight=170, color=RED]
Apple [weight=190, color=RED]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)