johnmills
johnmills

Reputation: 63

Filter a List based on words from String Array

I have a result set having List<Employees> sent by another application.

class Employee{
    Long id;
    String name;
    String gender;
    List<String> projects;
    // Getters
    // Setters
}

I need to write a method or lambda expression to filter the List using a bunch of query words (String[]) passed from the UI.

Any word in the String[] can match any variable (id, name, gender, projects). All List which have a match should be returned. part of name should also match e.g.: "john" should match List 1 and 3 in the example.

List<Employee> filter (empList, queryWords) {
    // code
}

Can you point me in the right direction to achive this?

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

Upvotes: 1

Views: 2661

Answers (3)

jb.
jb.

Reputation: 10351

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;
}

Upvotes: 1

Mureinik
Mureinik

Reputation: 312219

You could convert the query words array to a Set, create a Set of properties from all the employee's members, and use retainAll to determine which employees have at least one of the query words:

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());
}

EDIT:
As JB Nizet commented, the retainAll can be elegantly replaced with an anyMatch expression:

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());
}

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692181

Write a method

private boolean employeeMatchesWord(Employee employee, String word)

that returns true if at least one field of the employee matches the given word.

Then use

return empList.stream()
              .filter(employee -> Arrays.stream(queryWords)
                                        .anyMatch(word -> employeeMatchesWord(employee, word))
              .collect(Collectors.toList());

Upvotes: 1

Related Questions