4

I am trying to use an object's method in a Java stream to set the filter. For example:

//Full of Test objects that have a method getPredicate() that returns a valid predicate (ex d -> d.getName.equals("Test")). 
 ArrayList<Test> testArray = new ArrayList<Test>(); 

testArray
 .stream()  
 .filter(*CURRENTELEMENT*.getPredicate())  //Goal
 .forEach(System.out::println);

The return lambda from getPredicate() could be anything as the goal of this is to have a dynamic filter that can be set by the Test objects in this case.

Thanks in advance for any help! This is my first post so I hope I explained myself ok.

Edit/Update: Here is what the getPredicate() method looks like for a Test object:

    public Predicate<Test> getPredicate(String name, String id) {

    List<Predicate<Test>> allFilters = Arrays.asList();
    Predicate<Test> aggregateFilters;

        allFilters.add(d -> d.getName().equals(name));
        allFilters.add(d -> d.getId().equals(id));

        //Chain all filter predicates together using "or" method. 
            aggregateFilters= allFilters
                    .stream()
                    .reduce(d -> false, Predicate::or);

            // Returns a valid filter lambda expression
            // If I wasn't trying to get the aggregateFilters variable from this method, I could 
            // statically assign it and plug it right in and it works. ex. .filter(aggregateFilters)
            return aggregateFilters; 
}

Thanks again for your time and help.

4
  • 4
    What does getPredicate() return? It sounds like you just want e -> e.getPredicate().test(e). Commented Apr 3, 2018 at 20:57
  • It’s a bit odd for an instance to return a predicate; it may be simpler to have a method that returns boolean and use a method reference Commented Apr 3, 2018 at 21:16
  • 1
    you need to provide more details here, like how does Test look like at least and why do you think you need this dynamic setting of a Predicate. this might be an interesting question, but its just poorly written. Commented Apr 4, 2018 at 6:57
  • The return lambda from getPredicate() could be anything. No it can't. It can only have a type Predicate if you want to use that return value as a predicate in the filter above. Furthermore, since each Test object is returning a predicate, then each time that object is tested against the predicate, it will always be true...wat? Commented Apr 4, 2018 at 15:43

1 Answer 1

1

You can do:

testArray.stream().filter(e -> e.getPredicate("some_name", "some_id").test(e)).forEach(System.out::println);

where Test is defined as in your question.

Why I do not see is the point of getPredicate method to be instance member of Test class since it does not depends on any instance member. Instead you should consider it as a static method, that way you will have:

testArray.stream().filter(e -> Test.getPredicate("some_name", "some_id").test(e)).forEach(System.out::println);

Note: a list initialized as Arrays.asList() will throw a UnsupportedOperationException in add method since it is an immutable object. Instead you can initialize it as new ArrayList<>().

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

3 Comments

maybe you didn't see it but the OP did put "Test objects that have a method getPredicate() that returns a valid predicate (ex d -> d.getName.equals("Test"))." sounds like getPredicate returns a function which when evaluated will return a boolean as opposed to it being boolean getPredicate().
@Aominè it seems that the goal here is to somehow distinguish lambda expressions, which is simply not possible
@Eugene Right, I see. completely agree with your comment under the question.

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.