java对象的list取索引值,如何通过Java列表中的对象属性获取
索引
I would like to get the index of an object in a list by its property in Java.
Example:
List list = new ArrayList<>();
list.add(new MyObj("Ram");
list.add(new MyObj("Girish");
list.add(new MyObj("Ajith");
list.add(new MyObj("Sai");java中index是什么意思
public class MyObj {
public String name;
public MyObj(String name){
this.name=name;
}
}
Now, I would like to the get the index of an Object which contains the name as "Girish". Please do let me know the code in JAVA.
解决⽅案
If you want a solution with stream use this one:
int index = IntStream.range(0, list.size())
.filter(i -> (i).name.equals(searchName))
.findFirst();
.orElse(-1);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论