Using predecessor classes in collections

I have a method

<T> void f (Collection<T> c, T t)

Alas, sometimes I need to pass arg Collection as the first argument , but Collection <P> , where P is the predecessor of T , i.e. There is a method P , which returns T . So, how do I change my method to work with Collection <P> and Collection <T> (in the latter case, the getter is personal)?

For instance,

<T> boolean f (Collection<T> c, T t) {
  for (T x : c) if (x.equals(t)) return true;
  return false;
}
class Z { String a, b; }

I want to use f to search Collection <Z> with a or b

f(Collection<Z> c1 ???searching by a???, "foo")
f(Collection<Z> c2 ???searching by b???, "bar")

T P; , P T ( : Z , ).

Collection <T> .

- : Lisp .

+3
2

, - . :

interface Predicate<T> {
     public boolean test(T element);
}

:

 public <T> boolean f(Collection<T> c, Predicate<T> p) {
     for (T x : c) if (p.test(x)) return true;
     return false;
 }

:

Collection<T> ts;
f(ts, new Predicate<T> () { 
            public boolean test(T element) { 
                      return element.equals("foo"); 
            }
 });

Collection<P> ps;
f(ps, new Predicate<P> () { 
            public boolean test(P element) { 
                      return element.getT().equals("foo"); 
            }
});

, . , Java 8, .

+2

, "Holder" , "Stuff" - gotten, :
EDIT: instanceof.

public <H, T> boolean contains (Collection<H> c, T t) {
  if (c instanceof Collection<T>)
  {
    for (T x : c) if (x.equals(t)) return true;
    return false;
  }
  //else
  for (H x : c) if (x.getStuff().equals(t)) return true;
  return false;
}
-1

All Articles