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

时间:2018-12-26 09:11:42

标签: java if-statement

我想根据4个参数过滤列表。但是我无法获得想要的预期输出。

如果多个参数不为空或为null,则我希望多个if-else关系成为AND。
如果只有一个参数具有值,则该关系将为OR。

情况1:当tagUId不为空且状态不为空时

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

预期输出:
只有tagUId标记为“ 12”且状态为true的标记才会包含在结果列表中。

情况2:当tagUId不为空,状态不为null,pullStatus不为空

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

预期输出:
仅具有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;
}

1 个答案:

答案 0 :(得分:0)

尝试一下;

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;

}