Get last element of Stream/List in a one-liner

前端 未结 7 2044
时光取名叫无心
时光取名叫无心 2020-12-04 15:50

How can I get the last element of a stream or list in the following code?

Where data.careas is a List:

CArea f         


        
7条回答
  •  自闭症患者
    2020-12-04 16:42

    You can also use skip() function as below...

    long count = data.careas.count();
    CArea last = data.careas.stream().skip(count - 1).findFirst().get();
    

    it's super simple to use.

提交回复
热议问题