Java 检查包含对象的列表中是否存在值的条件,
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23336169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Condition to check if a value exists in list containing objects,
提问by Raghavendra
I am trying to write an if condition to check a value exists in a list containing many objects, Here is my code:
我正在尝试编写一个 if 条件来检查包含许多对象的列表中是否存在一个值,这是我的代码:
List<TeacherInfo> teacherInfo=ServiceManager.getHelperService(TeacherManagementHelper.class, request, response).getTeacherInfoId();
if(teacherInfo.contains(inputParam))
{
out2.println("<font color=red>");
out2.println("Id Not Available");
out2.println("</font>");
}
else
{
out2.println("<font color=green>");
out2.println("Id Available");
out2.println("</font>");
}
after executing 1st sentence getTeacherInfoId()
method successfully returns a list of objects, in those objects I want to check any object has a value same as inputParam
. Is my above code right ? if wrong please help me .
执行第一句getTeacherInfoId()
方法成功返回对象列表后,在这些对象中,我想检查任何对象的值与inputParam
. 我上面的代码对吗?如果错了请帮助我。
采纳答案by Joffrey
contains(Object o)
is internally based on equals
between objects of your list and your input, as stated by the doc.
contains(Object o)
如 doc 所述,内部基于equals
您的列表对象和您的输入之间。
Since you said that inputParam
is an integer, then the current state of your code can't work because you compare an integer to TeacherInfo
objects, so they won't ever be equal. I believe you want to compare inputParam
to one particular field of TeacherInfo
objects.
既然您说这inputParam
是一个integer,那么您的代码的当前状态就无法工作,因为您将一个 integer 与TeacherInfo
objects进行比较,因此它们永远不会相等。我相信您想与inputParam
一个特定的TeacherInfo
对象领域进行比较。
If you're using Java 8, you can use the stream API instead of contains()
:
如果您使用的是 Java 8,则可以使用流 API 而不是contains()
:
List<TeacherInfo> teacherInfo=ServiceManager.getHelperService(TeacherManagementHelper.class, request, response).getTeacherInfoId();
if (teacherInfo.stream().anyMatch(ti -> ti.getId() == inputParam)) {
// contains the id
} else {
// does not contain the id
}
For previous java versions, an alternative to contains()
would be to iterate over your list and compare manually your integer to the TeacherInfo
's field:
对于以前的 Java 版本,替代方法contains()
是遍历您的列表并手动将您的整数与TeacherInfo
's 字段进行比较:
private static boolean containsTeacherId(List<TeacherInfo> teacherInfos, int id) {
for (TeacherInfo ti : teacherInfos) {
if (ti.getId() == inputParam) { // I used getId(), replace that by the accessor you actually need
return true;
}
}
return false;
}
Then:
然后:
List<TeacherInfo> teacherInfo=ServiceManager.getHelperService(TeacherManagementHelper.class, request, response).getTeacherInfoId();
if (containsTeacherId(teacherInfo, inputParam)) {
// contains the id
} else {
// does not contain the id
}
Note:If you don't need other information than the ID itself, I'd rather suggest to return the list of IDs from a method called getTeacherIds()
, especially if this information comes from a DB.
注意:如果除了 ID 本身不需要其他信息,我宁愿建议从名为 的方法返回 ID 列表getTeacherIds()
,特别是如果此信息来自数据库。
回答by coder1608
No it won't work at all. you should iterate the 'teacherInfo' list and you need to override the compare()and hashvalue() of object class.
不,这根本行不通。您应该迭代“teacherInfo”列表,并且需要覆盖对象类的 compare() 和 hashvalue()。
回答by Parul S
You would need to iterate over the list teacherInfo and compare each element of that list with inputParam.
您需要遍历列表teacherInfo 并将该列表的每个元素与inputParam 进行比较。
Below is a small demo code that might help you.
下面是一个可能对您有所帮助的小演示代码。
I have created a testerInfo analogous to your teacherInfo and param analogous to your inputParam. I hope it helps.
我创建了一个类似于您的teacherInfo 的 testerInfo 和类似于您的 inputParam 的 param。我希望它有帮助。
Tester.java
/**
*
*/
package com.demo;
/**
* @author Parul
*
*/
public class Tester {
private int id;
private String name;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public Tester(int id, String name) {
this.id = id;
this.name = name;
}
public Tester() {
}
}
Demo.java
演示.java
/**
*
*/
package com.demo;
import java.util.ArrayList;
import java.util.List;
/**
* @author Parul
*
*/
public class Demo {
public static void main(String [] args){
List<Tester> testerInfo=new ArrayList<Tester>();
testerInfo.add(new Tester(1,"Java"));
testerInfo.add(new Tester(2,"C++"));
testerInfo.add(new Tester(3,"Python"));
testerInfo.add(new Tester(4,"C"));
Tester tester=null;
int param=2;
for(int i=0;i<testerInfo.size();i++){
tester=testerInfo.get(i);
if(tester.getId()==param){
System.out.println("param found: "+tester.getName());
break;
}
}
}
}
OUTPUT
param found: C++