0

I need to remove some elements from a list inside an hashmap. Old way works. New way won't work, seems do not remove requested elements.

Logging in Old way I never obtain "find after!", as expected. With New way I obtains some "find after!" :(

Old way:

workingMarketCards.forEach((k, v) -> {
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == oc.getMasterCardId()) {
            System.out.println("found before!");
        }
    }
    ArrayList<FixedPriceInsertion> toBeRemoved = new ArrayList<>();
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == (long) (oc.getMasterCardId())) {
            toBeRemoved.add(fpi);
        }
    }
   
    for (FixedPriceInsertion fpi : toBeRemoved) {
        v.remove(fpi);
    }
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == oc.getMasterCardId()) {
            System.out.println("found after!");
        }
    }
});

New way:

workingMarketCards.forEach((k, v) -> {
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == oc.getMasterCardId()) {
            System.out.println("found before!");
        }
    }
    v.stream().filter(s -> s.getMasterId() == (long) (oc.getMasterCardId()));
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == oc.getMasterCardId()) {
            System.out.println("found after!");
        }
    }
});

Why? What wrong in my code?

2
  • .filter() does not remove items from the list, it "returns a stream consisting of the elements of this stream that match the given predicate". look at .removeIf() Commented Sep 10, 2020 at 17:26
  • also use .equals() instead of == when comparing objects. Commented Sep 10, 2020 at 17:27

2 Answers 2

3

You want removeIf():

v.removeIf(s -> s.getMasterId() == (long) (oc.getMasterCardId()));

v.stream().filter() does nothing to v; it only affects the stream of elements generated from v.


It's not clear what type getMasterId() returns, but caution is advised using == for comparing things other than primitives; always use .equals() when comparing Objects.

Sign up to request clarification or add additional context in comments.

1 Comment

.equals() is a good point, this is a sample code to understood java lambda expressions not a production code. Thanks.
1

The filter doesn't modify the values in the variable s. It only send the filtered stream to the next chain methods and since you don't have anything after that it won't do anything.

What you need to do is to use the filter to remove the values and the collect them.

ArrayList<FixedPriceInsertion> v2 = v.stream()
                        .filter(s -> s.getMasterId() != (long) (oc.getMasterCardId()))
                        .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.