Iterate to find a Map entry at an index?

前端 未结 4 1832
小鲜肉
小鲜肉 2020-12-20 13:35

I have a LinkedHashMap. I want to get the Foo at index N. Is there a better way of doing this besides iterating until I find it?:

int target = N;
int index =         


        
4条回答
  •  一整个雨季
    2020-12-20 13:50

    Guava library can help in this case:

    public static  T com.google.common.collect.Iterables.get(Iterable iterable, int position)
    

    see javadoc: Iterables.get

    For your case the code can be like this:

    Iterables.get(foos.values(), N);
    

提交回复
热议问题