Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在与java对象的所有成员匹配的java对象列表中执行关键字搜索_Java_Search_Collections - Fatal编程技术网

如何在与java对象的所有成员匹配的java对象列表中执行关键字搜索

如何在与java对象的所有成员匹配的java对象列表中执行关键字搜索,java,search,collections,Java,Search,Collections,我有一个列表,其中包含多个成员,如id、姓名、名称、级别、角色、域、帐户等。 我想搜索整个列表中Employee的所有成员的关键字,并返回包含所有匹配实例的列表Employee 以下是我的员工课程的示例: public class Employee { public String name { get; set; } public String email { get; set; } public String designation{ get; set; } p

我有一个
列表
,其中包含多个成员,如id、姓名、名称、级别、角色、域、帐户等。 我想搜索整个列表中
Employee
的所有成员的关键字,并返回包含所有匹配实例的列表
Employee

以下是我的
员工
课程的示例:

public class Employee
{
    public String name { get; set; }
    public String email { get; set; }
    public String designation{ get; set; }
    public String level { get; set; }
    public String domain { get; set; }
    public String role { get; set; }
}
public class Employee{

/* Instance Members */

public boolean checkMatch(String keyword) throws IllegalAccessException {
    for(Field field: this.getClass().getDeclaredFields()){
        String value = (String) field.get(this);
        if(value.equalsIgnoreCase(keyword))
            return true;
    }
   return false;
}

}
更像是沃尔玛或亚马逊等电子商务网站上的关键词搜索

我不想在循环中编写一系列if语句来匹配每个成员。我认为一种方法是创建另一个字符串,该字符串保存
Employee
对象中每个成员的连接值,然后使用Java8流进行搜索,但我认为还有更好的方法。
还有什么建议吗?

您可以使用
反射
循环遍历所有字段并比较值,因此在
员工
类中创建此方法:

public class Employee
{
    public String name { get; set; }
    public String email { get; set; }
    public String designation{ get; set; }
    public String level { get; set; }
    public String domain { get; set; }
    public String role { get; set; }
}
public class Employee{

/* Instance Members */

public boolean checkMatch(String keyword) throws IllegalAccessException {
    for(Field field: this.getClass().getDeclaredFields()){
        String value = (String) field.get(this);
        if(value.equalsIgnoreCase(keyword))
            return true;
    }
   return false;
}

}
用法:

public static void main(String[] args) throws IllegalAccessException {
    List<Employee> employees = new ArrayList<>();
    String keyword = "Research";
    employees.add(new Employee("john","john@doe.com","Manager","3rd","Research","Senior"));
    for(Employee e: employees){
        if(e.checkMatch(keyword)){
            System.out.println("Yayy! Record Found");
        }
    }
}
publicstaticvoidmain(字符串[]args)抛出IllegaccessException{
List employees=new ArrayList();
String关键字=“研究”;
员工。添加(新员工(“john”john@doe.com","经理","三等","研究","高级",;
对于(员工e:员工){
如果(例如,检查匹配(关键字)){
System.out.println(“找到了Yayy!记录”);
}
}
}

您可以使用
反射
循环所有字段并比较值,因此在
员工
类中创建此方法:

public class Employee
{
    public String name { get; set; }
    public String email { get; set; }
    public String designation{ get; set; }
    public String level { get; set; }
    public String domain { get; set; }
    public String role { get; set; }
}
public class Employee{

/* Instance Members */

public boolean checkMatch(String keyword) throws IllegalAccessException {
    for(Field field: this.getClass().getDeclaredFields()){
        String value = (String) field.get(this);
        if(value.equalsIgnoreCase(keyword))
            return true;
    }
   return false;
}

}
用法:

public static void main(String[] args) throws IllegalAccessException {
    List<Employee> employees = new ArrayList<>();
    String keyword = "Research";
    employees.add(new Employee("john","john@doe.com","Manager","3rd","Research","Senior"));
    for(Employee e: employees){
        if(e.checkMatch(keyword)){
            System.out.println("Yayy! Record Found");
        }
    }
}
publicstaticvoidmain(字符串[]args)抛出IllegaccessException{
List employees=new ArrayList();
String关键字=“研究”;
员工。添加(新员工(“john”john@doe.com","经理","三等","研究","高级",;
对于(员工e:员工){
如果(例如,检查匹配(关键字)){
System.out.println(“找到了Yayy!记录”);
}
}
}

是的,在这种情况下,他可以进行检查,我只是想提供一个想法,在示例中处理所有这些事情只会使事情变得复杂。在这种情况下,他可以进行检查,我只是想提供一个想法,在示例中处理所有这些事情只会使事情变得复杂