我的结果集中有另一个应用程序发送的List<Employees>
。
class Employee{
Long id;
String name;
String gender;
List<String> projects;
// Getters
// Setters
}
我需要编写一个方法或lambda表达式来使用从UI传递的一堆查询词(List
)来过滤String[]
。
String[]
中的任何字词都可以匹配任何变量(ID,名称,性别,项目)。应返回所有匹配的列表。名称的一部分也应匹配,例如:“john”应与示例中的列表1和3匹配。
List<Employee> filter (empList, queryWords) {
// code
}
你能指出我正确的方向来实现这个目标吗?
example:
List:
1. 121, john doe , male , (proj1)
2. 125, sam , female, (proj4 proj5 proj9)
3. 129, john lam , male , (proj1 proj2 proj5)
4. 143, peter pan , male , (proj4 proj8)
5. 151, linda , female, (proj8 proj7 proj3 proj11)
Search Query Words:
1. "female" "proj3"- should return only No.5
2. "proj5" - should return only No.2 and 3
3. "john" - should return No.1 and 3
4. "pan" - should return No.4
答案 0 :(得分:1)
写一个方法
private boolean employeeMatchesWord(Employee employee, String word)
如果员工的至少一个字段与给定的单词匹配,则返回true。
然后使用
return empList.stream()
.filter(employee -> Arrays.stream(queryWords)
.anyMatch(word -> employeeMatchesWord(employee, word))
.collect(Collectors.toList());
答案 1 :(得分:1)
public List<Employee> filter(empList, queryWords){
List<Employee> result = new ArrayList<Employee>();
// look at each employee in the list
for(Employee employee : empList){
// look at each query string
for(String queryWord : queryWords){
// if any of the employee fields matches the query word,
// add it to our list and move to next employee
if(employee.name.equals(queryWord) ||
employee.gender.equals(queryWord) ||
employee.id.toString().equals(queryWord) ||
isQueryInList(queryWord, employee.projects)) {
// add it to your results
result.add(employee);
// quit looking at the rest of the queryWords,
// we found one, thats enough, move on to the next employee
break;
}
}
}
return result;
}
private boolean IsQueryInList(String queryWord, List<String> items){
//check each item in the list to see if it matches the queryWord
for(String item : items){
if(queryWord.equals(item)) {
return true;
}
}
//if we didn't find any item that matches, return false
return false;
}
答案 2 :(得分:1)
您可以将查询字数组转换为Set
,从所有员工的成员创建Set
个属性,并使用retainAll
来确定哪些员工拥有至少有一个查询词:
public static List<Employee> filter (List<Employee> empList, String[] queryWords) {
Set<String> queryWordsSet = new HashSet<>(Arrays.asList(queryWords));
return empList.stream().filter(e -> {
Set<String> properties = new HashSet<>(e.getProjects());
properties.addAll
(Arrays.asList(e.getId().toString(), e.getName(), e.getGender()));
properties.retainAll(queryWordsSet);
return !properties.isEmpty();
}).collect(Collectors.toList());
}
编辑:
正如JB Nizet评论的那样,retainAll
可以用anyMatch
表达式优雅地替换:
public static List<Employee> filter (List<Employee> empList, String[] queryWords) {
Set<String> queryWordsSet = new HashSet<>(Arrays.asList(queryWords));
return empList.stream().filter(e -> {
Set<String> properties = new HashSet<>(e.getProjects());
properties.addAll
(Arrays.asList(e.getId().toString(), e.getName(), e.getGender()));
return properties.stream().anyMatch(queryWordsSet::contains);
}).collect(Collectors.toList());
}