How to filter a list with multiple criteria?

I want to filter a list based on 4 parameters. But I cannot get the expected output that I want.

If more than one parameter is not empty or null, I want the multiple if-else relation become AND.
If only one parameter has value, then the relation will be OR.

Case 1: When tagUId is not empty and status is not null

Input:
title = “”, tagUId = “12”, status = true, pullStatus = null

Expected Output:
Only tag with tagUId contains “12” AND status is true will be included into the result list.

Case 2: When tagUId is not empty, status is not null, pullStatus is not null

Input:
title = “”, tagUId = “12”, status = true, pullStatus = false

Expected Output:
Only tag with tagUId contains “12” AND status is true AND pullStatus is false will be included into the result list.

public static ArrayList<TagEntity> SearchTagsBy(String title, String tagUId, Boolean status, Boolean pullStatus){
    HashSet<TagEntity> result = new HashSet<>();
    ArrayList<TagEntity> tags = GetTagList();

    if (title.isEmpty() && tagUId.isEmpty() && status == null && pullStatus == null) {
        result.addAll(tags); //default parameters == show all tag
    }else{

        if(!title.isEmpty()){
            result.addAll(FilterTitle(tags, title));
        }

        if(!tagUId.isEmpty()){
            result.addAll(FilterTagUId(tags, tagUId));
        }

        if(status != null){
            result.addAll(FilterStatus(tags, status));
        }

        if(pullStatus != null){
            result.addAll(FilterPullStatus(tags, pullStatus));
        }

    }

    return new ArrayList<>(result);
}

private static Collection<? extends TagEntity> FilterPullStatus(ArrayList<TagEntity> tags, Boolean pullStatus) {
    HashSet<TagEntity> result = new HashSet<>();

    for (TagEntity tag: tags) {
        if (tag.getHasPulled().equals(pullStatus)){
            result.add(tag);
        }

    }

    return result;
}

private static Collection<? extends TagEntity> FilterStatus(ArrayList<TagEntity> tags, Boolean status) {
    HashSet<TagEntity> result = new HashSet<>();

    for (TagEntity tag: tags) {
        if (tag.getStatus().equals(status)){
            result.add(tag);
        }

    }

    return result;
}

private static Collection<? extends TagEntity> FilterTagUId(ArrayList<TagEntity> tags, String tagUId) {
    HashSet<TagEntity> result = new HashSet<>();

    for (TagEntity tag: tags) {
        if (tag.getUId().contains(tagUId)){
            result.add(tag);
        }

    }

    return result;
}

private static HashSet<TagEntity> FilterTitle(ArrayList<TagEntity> tags, String title){
    HashSet<TagEntity> result = new HashSet<>();

    for (TagEntity tag: tags) {
        if (tag.getTitle().contains(title)){
            result.add(tag);
        }

    }

    return result;
}

Answer

Try this;

public static ArrayList<TagEntity> SearchTagsBy(String title, String tagUId, Boolean status, Boolean pullStatus){

    ArrayList<TagEntity> result = new ArrayList<>();
    ArrayList<TagEntity> tags = GetTagList();

    for(TagEntity tag: tags)
    {
        boolean checkTitle, checkUId, checkStatus, checkPullStatus;

        if(title.isEmpty()) checkTitle = true; else checkTitle = tag.getTitle().contains(title);

        if(tagUId.isEmpty()) checkUId = true; else checkUId = tag.getUId().contains(tagUId);

        if(status == null)checkStatus = true; else checkStatus = tag.getStatus().equals(status);

        if(pullStatus == null)checkPullStatus = true; else checkPullStatus = tag.getHasPulled().equals(pullStatus);

       if( checkTitle && checkUId && checkStatus && checkPullStatus)
             result.add(tag);
    }

    return result;

}

Leave a Reply

Your email address will not be published. Required fields are marked *