📜 ⬆️ ⬇️

Use generic wildcards to improve the convenience of the Java API

Good day!

This post is for those who are working on the next API in the Java language, or are trying to improve an existing one. Here will be given simple advice, how with the help of constructions ? extends T ? extends T and ? super T ? super T can greatly enhance the usability of your interface.

Go straight to the point

Source API


Suppose you have an interface of a certain object storage, parameterized, say, two types: a key type ( K ) and a value type ( V ). The interface defines a set of methods for working with data in the repository:
')
 public interface MyObjectStore<K, V> { /** *       . * * @param key . * @param value . */ void put(K key, V value); /** *       . * * @param key . * @return   null. */ @Nullable V get(K key); /** *    -  . * * @param entries   -. */ void putAll(Map<K, V> entries); /** *        * . * * @param keys  . * @return  -. */ Map<K, V> getAll(Collection<K> keys); /** *     ,  *   (). * * @param p    . * @return ,  . */ Collection<V> getAll(Predicate<V> p); ... //    } 

Predicate Definition
 interface Predicate<E> { /** *  true,    * , false   . * * @param exp   . * @return true,  ; false,  . */ boolean apply(E exp); } 


The interface looks quite adequate and logical, the user can easily write simple code to work with the repository:

 MyObjectStore<Long, Car> carsStore = ...; carsStore.put(20334L, new Car("BMW", "X5", 2013)); Car c = carsStore.get(222L); ... 

However, in slightly less trivial cases, the client of your API will face unpleasant restrictions.

Use ? super T ? super T


Take the last method that reads the values ​​that satisfy the predicate. What could be wrong with him? We take, and we write:

 Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { ... //      . } }); 

But the fact is that our client already has a predicate for choosing cars. Only it is not parameterized by the Car class, but by the Vehicle class, from which Car inherits. He may try to cram
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);


Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
 Predicate  Predicate,      : 

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
Predicate Predicate, :

no suitable method found for getAll(Predicate<Vehicle>)
, , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
! :

Collection<V> getAll(Predicate<? super V> p);
Predicate<? super V> " V V ( Object)". , . Vehicle :

MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
, .

? extends T
, . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
, , , , wildcard ? extends T :

void putAll(Map<? extends K, ? extends V> entries);
Map<? extends K, ? extends V> " K K V V".


PECS - Producer Extends Consumer Super
, , , .

Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

(, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
, ? : , - , , . , , T.

Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

, , - , , ( , ) - .

- wildcard- , wildcard- .

PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
getAll(Collection) - , .

Map<K, V> getAll(Collection<K> keys);



Map<K, V> getAll(Collection<? extends K> keys);
, API! (, !)


. - :

interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
, , , :

interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
, ouput-, ( Java ).


PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
  1. Predicate Predicate, :

    no suitable method found for getAll(Predicate<Vehicle>)
    , , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

    final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
    ! :

    Collection<V> getAll(Predicate<? super V> p);
    Predicate<? super V> " V V ( Object)". , . Vehicle :

    MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
    , .

    ? extends T
    , . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

    MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
    , , , , wildcard ? extends T :

    void putAll(Map<? extends K, ? extends V> entries);
    Map<? extends K, ? extends V> " K K V V".


    PECS - Producer Extends Consumer Super
    , , , .

    Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

    (, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
    , ? : , - , , . , , T.

    Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

    , , - , , ( , ) - .

    - wildcard- , wildcard- .

    PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
    getAll(Collection) - , .

    Map<K, V> getAll(Collection<K> keys);



    Map<K, V> getAll(Collection<? extends K> keys);
    , API! (, !)


    . - :

    interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
    , , , :

    interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
    , ouput-, ( Java ).


    PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


    Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections
  2. Predicate Predicate, :

    no suitable method found for getAll(Predicate<Vehicle>)
    , , Vehicle - Car . Car , , , Vehicle , Car ! Vehicle Car . , , , :

    final Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(new Predicate<Car>() { @Override public boolean apply(Car exp) { return vp.apply(exp); } });
    ! :

    Collection<V> getAll(Predicate<? super V> p);
    Predicate<? super V> " V V ( Object)". , . Vehicle :

    MyObjectStore<Long, Car> carsStore = ...; Predicate<Vehicle> vp = mgr.getVehiclePredicate(); Collection<Car> cars = carsStore.getAll(vp);
    , .

    ? extends T
    , . , , ? extends T . : MyObjectStore<Long, Vehicle> , Map<Long, Car> ( Car - Vehicle ), :

    MyObjectStore<Long, Vehicle> carsStore = ...; Map<Long, Car> cars = new HashMap<Long, Car>(2); cars.put(1L, new Car("Audi", "A6", 2011)); cars.put(2L, new Car("Honda", "Civic", 2012)); carsStore.putAll(cars); // .
    , , , , wildcard ? extends T :

    void putAll(Map<? extends K, ? extends V> entries);
    Map<? extends K, ? extends V> " K K V V".


    PECS - Producer Extends Consumer Super
    , , , .

    Joshua Bloch PECS (Producer Extends Consumer Super) , Java Generics and Collections (Maurice Naftalin, Philip Wadler) - Get and Put Principle . PECS, . :

    (, Collection Predicate), , - ( producer ), ? extends T , - ( consumer ), ? super T .
    , ? : , - , , . , , T.

    Predicate - ( getAll(Predicate) T), Map<K, V> - ( putAll(Map<K, V>) T - T K V - ).

    , , - , , ( , ) - .

    - wildcard- , wildcard- .

    PECS-, MyObjectStore , . put(K, V) get(K) (.. ); putAll(Map<? extends K, ? extends V>) getAll(Predicate<? super V>) , ;
    getAll(Collection) - , .

    Map<K, V> getAll(Collection<K> keys);



    Map<K, V> getAll(Collection<? extends K> keys);
    , API! (, !)


    . - :

    interface Factory<T> { /** * . * * @param args . * @return . */ T create(Object... args); }
    , , , :

    interface Cloner<T> { /** * . * * @param obj . * @return . */ T clone(T obj); }
    , ouput-, ( Java ).


    PECS (Producer Extends Consumer Super) API Java. , , API. , , , PECS , .


    Joshua Bloch - Effective Java (2nd Edition) Maurice Naftalin, Philip Wadler - Java Generics and Collections

Source: https://habr.com/ru/post/207360/


All Articles