简体   繁体   English

通过流 Java 8 迭代从列表中删除元素

[英]Remove elements from a list by iterating via stream Java 8

I want to remove some elements from my list of objects based on certain conditions.我想根据某些条件从我的对象列表中删除一些元素。 Can I do it with Java 8 streams?我可以用 Java 8 流来做吗?

public static void doAction(List<Hunt> list) {
    for (ListIterator<Hunt> iter = list.listIterator(); iter.hasNext(); ) {
        Hunt h = iter.next();
        if (h.getTokens().contains("INC")) {
            String[] tags = h.getTokens().split(";");
            for (String tag : tags) {
                if (tag.contains("INC")) {
                    String value = tag.substring(tag.length() - 1);
                    if ("N".equals(value)) {
                        if (flag) {
                            if (!h.getPropertyA().equals(anotherObject.getPropertyAValue())) {
                                iter.remove();
                            }
                        }
                        else {
                            if (!h.getPropertyB().equals(anotherObject.getPropertyBValue)) {
                                iter.remove();
                            }
                        }
                    }
                }
            }
        }
    }
}

I think you can do it with stream by using removeIf and some condition :我认为你可以通过使用removeIf和一些条件来removeIf流:

list.removeIf(h -> h.getTokens().contains("INC") &&
        Arrays.stream(h.getTokens().split(";"))
                .filter(tag -> tag.contains("INC"))
                .map(tag -> tag.substring(tag.length() - 1))
                .filter("N"::equals)
                .anyMatch(v ->
                        (flag && !h.getPropertyA().equals(anotherObject.getPropertyAValue())) ||
                                (!flag && !h.getPropertyB().equals(anotherObject.getPropertyBValue()))));

Or you can separated the condition in a separated method, for example :或者您可以在分离的方法中分离条件,例如:

static void doAction(List<Hunt> list) {
    list.removeIf(MyClass::isCorrect);
}

private static boolean isCorrect(Hunt h) {
    return h.getTokens().contains("INC") &&
            Arrays.stream(h.getTokens().split(";"))
                    .filter(tag -> tag.contains("INC"))
                    .map(tag -> tag.substring(tag.length() - 1))
                    .filter("N"::equals)
                    .anyMatch(v ->
                            (flag && !h.getPropertyA().equals(anotherObject.getPropertyAValue())) ||
                            (!flag && !h.getPropertyB().equals(anotherObject.getPropertyBValue())));
}

Or as @Tom Hawtin - tackline suggest, you can use :或者作为@Tom Hawtin - 主攻建议,您可以使用:

static void doAction(List<Hunt> list) {
    list.removeIf(h -> h.getTokens().contains("INC") &&
            Arrays.stream(h.getTokens().split(";"))
                    .anyMatch(tag -> tag.contains("INC") && tag.endsWith("N")
                            && !(flag ? h.getPropertyA().equals(anotherObject.getPropertyAValue())
                            : h.getPropertyB().equals(anotherObject.getPropertyBValue()))));
}

IMO stream API wont help you in this case!在这种情况下,IMO 流 API 不会帮助您! And this style of code is more readable in compared with stream based version.与基于流的版本相比,这种风格的代码更具可读性。 however I think you can make improvement by changing a bit.但是我认为你可以通过改变一点来改进。

  • Remove temporary variables such as String[] tags = h.getTokens().split(";");删除临时变量,如String[] tags = h.getTokens().split(";"); and String value = tag.substring(tag.length() - 1);String value = tag.substring(tag.length() - 1);

  • Combine some conditions with together.将一些条件结合在一起。

  • Use tag.charAt(tag.length() - 1) instead of tag.substring(tag.length() - 1);使用tag.charAt(tag.length() - 1)而不是tag.substring(tag.length() - 1);

     public static void doAction(List<Hunt> list) { for (ListIterator<Hunt> iter = list.listIterator(); iter.hasNext(); ) { Hunt h = iter.next(); if (h.getTokens().contains("INC")) { for (String tag : h.getTokens().split(";")) { if (tag.contains("INC") && tag.charAt(tag.length() - 1) == 'N') { if (flag) { if (!h.getPropertyA().equals(anotherObject.getPropertyAValue())) { iter.remove(); } } else { if (!h.getPropertyB().equals(anotherObject.getPropertyBValue)) { iter.remove(); } } } } } } }

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

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