Best way to get value from Collection by index

前端 未结 10 1542
旧时难觅i
旧时难觅i 2020-12-05 17:08

What is the best way to get value from java.util.Collection by index?

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 17:32

    In general, there is no good way, as Collections are not guaranteed to have fixed indices. Yes, you can iterate through them, which is how toArray (and other functions) work. But the iteration order isn't necessarily fixed, and if you're trying to index into a general Collection, you're probably doing something wrong. It would make more sense to index into a List.

提交回复
热议问题