Stream Way to get index of first element matching boolean

前端 未结 6 1054
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 09:12

I have a List. I want to get the index of the (first) user in the stream with a particular username. I don\'t want to actually require the Us

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 09:28

    Try This:

    IntStream.range(0, users.size())
        .filter(userInd-> users.get(userInd).getName().equals(username))
        .findFirst()
        .getAsInt();
    

提交回复
热议问题