简体   繁体   中英

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?

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 :

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 :

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! 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(";"); and 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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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