0

I have JUnit tests (which I cannot modify) and have to write a program in Java 8. The part of the tests that I'm interested in is:

Set<Customer> mexicans = shop.filter(Customer.class, c -> c.getCountry().equals("Mexico"));
Set<Product> goldenWaffles = shop.filter(Product.class, p -> p.getBrand().equals("Golden") && p.getName().contains("Waffles"));

So the second parameter of filter function is lambda expression, which I would take as parameter of type Predicate<Product> or Predicate<Customer> (am I right here?). The problem is: I need to be able to take them both and I don't know how to do it. Has it something to do with the first parameter?

2
  • What kind of object are you trying to filter? Presumably a Customer can't be a golden waffle, and a Product can't be a Mexican; so what would it mean to apply both filters? Commented May 26, 2014 at 8:18
  • The filter method should be able to filter Customers and Products. It should probably decide what filtered set I want (Customers or Products) from the first parameter Commented May 26, 2014 at 8:34

1 Answer 1

2

Probably you are looking for something like

public <T> void method(Class<T> clazz, Predicate<T> predicate) {
    // ...
    final Set<T> result = shop.filter(clazz, predicate);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Did you mean for this to return Set<T> instead of void?
Depends on whether to work with the result of the method, or simply check for "not emptiness" or whatever.
Not sure if I understand you correctly, but public Set<Product> filter(Class<Product> clazz, Predicate<Product> predicate) and public Set<Customer> filter(Class<Customer> clazz, Predicate<Customer> predicate) creates same erasure error

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.