Java get last element of a collection

前端 未结 10 790
你的背包
你的背包 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:16

    There isn't a last() or first() method in a Collection interface. For getting the last method, you can either do get(size() - 1) on a List or reverse the List and do get(0). I don't see a need to have last() method in any Collection API unless you are dealing with Stacks or Queues

提交回复
热议问题