Sorting ArrayList with Lambda in Java 8

前端 未结 11 2224
北荒
北荒 2020-12-29 18:08

Could somebody show me a quick example how to sort an ArrayList alphabetically in Java 8 using the new lambda syntax.

11条回答
  •  忘掉有多难
    2020-12-29 18:43

    Suppose you have List of names(String) which you want to sort alphabetically.

    List result = names.stream().sorted(
                     Comparator.comparing(n->n.toString())).collect(Collectors.toList());
    

    its working perfectly.

提交回复
热议问题