简体   繁体   English

如何过滤具有多个条件的列表?

[英]How to filter a list with multiple criteria?

I want to filter a list based on 4 parameters. 我想根据4个参数过滤列表。 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. 如果多个参数不为空或为null,则我希望多个if-else关系成为AND。
If only one parameter has value, then the relation will be OR. 如果只有一个参数具有值,则该关系将为OR。

Case 1: When tagUId is not empty and status is not null 情况1:当tagUId不为空且status不为null时

Input: 输入:
title = "", tagUId = "12" , status = true , pullStatus = null title =“”, tagUId =“ 12”状态= true ,pullStatus = null

Expected Output: 预期产量:
Only tag with tagUId contains "12" AND status is true will be included into the result list. 只有tagUId标记为“ 12”且状态为true的标记才会包含在结果列表中。

Case 2: When tagUId is not empty, status is not null, pullStatus is not null 情况2:当tagUId不为空,状态不为null,pullStatus不为null时

Input: 输入:
title = "", tagUId = "12" , status = true , pullStatus = false title =“”, tagUId =“ 12”status = truepullStatus = false

Expected Output: 预期产量:
Only tag with tagUId contains "12" AND status is true AND pullStatus is false will be included into the result list. 仅具有tagUId的标记包含“ 12”,且状态为true,而pullStatus为false,将包含在结果列表中。

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

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM