Java get last element of a collection

前端 未结 10 823
你的背包
你的背包 2020-12-30 18:47

I have a collection, I want to get the last element of the collection. What\'s the most straighforward and fast way to do so?

One solution is to first toArray(), and

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 19:00

    Well one solution could be:

    list.get(list.size()-1)
    

    Edit: You have to convert the collection to a list before maybe like this: new ArrayList(coll)

提交回复
热议问题