Questions tagged [pecs]

A java generics acronym for "Producer (uses) Extends, Consumer (uses) Super"

A good design pattern to follow when specifying java generic parameters is that the producers of objects specify their parameters as <T extends Something>, while the consumers of those same objects specify their parameters as <T super Something>.

See What is PECS (Producer Extends Consumer Super)? for more.

30 questions
895
votes
16 answers

What is PECS (Producer Extends Consumer Super)?

I came across PECS (short for Producer extends and Consumer super) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super?
peakit
  • 28,597
  • 27
  • 63
  • 80
7
votes
2 answers

bounded generic method with 'super' type

As per a literature I read,we have juicy fruits implementign the following interface: public interface Juicy { Juice squeeze(); } Using bounded type variables, following method would taks a bunch of fruits and squeeze them all:
IUnknown
  • 9,301
  • 15
  • 50
  • 76
5
votes
4 answers

Why is adding a subclass a of type in a collection is illegal?

given this code snippet //Creates a list of List numbers List> num = new ArrayList>(); //Creates a list of List doubles List> doub = new ArrayList>(); //List of doubles …
KyelJmD
  • 4,682
  • 9
  • 54
  • 77
4
votes
1 answer

Is it possible to write a single method that accepts a generic parameter of varying abstraction?

As a followup to this question, is it possible to write a single method that adds a Dog to a suitable room? (In this example, it would accept either an Animal room or a Dog room.) Or am I forced to write two distinct methods as below? (I can't…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
4
votes
3 answers

Why Comparable and Comparator are consumers in PECS wildcard types in Java

In Effective Java, in the item "Use bounded wildcards to increase API flexibility", when talking about the usage of PECS (producer-extends, consumer-super), the author mentioned that: Comparables are always consumers, so you should generally use…
cxs1031
  • 150
  • 2
  • 9
4
votes
3 answers

Producer and Consumer with Generics in Java

I have this method to retrieve the objects which are instance of a given class: public class UtilitiesClass { public static final Collection get(Collection animals, Class clazz) { // returns the Animals…
Manuelarte
  • 1,658
  • 2
  • 28
  • 47
4
votes
3 answers

Java generics PECS

I know what is the meaning of PECS. Producer Extends,Consumer Super. the thing is how would I know if its a consumer or producer? Also does this code follow the "PECS" public class Tree { //List of branches for this tree private…
KyelJmD
  • 4,682
  • 9
  • 54
  • 77
3
votes
0 answers

Compilation fails when upper bound wildcard is used with a lower one

Situation: I'm making a configuration library, with a Config interface that represents the data, and a Parser interface like this: public interface Parser { C parse(File f); void parse(File f, D…
ElectronWill
  • 754
  • 9
  • 17
3
votes
1 answer

PECS does not work on return types with interface

Consider the following example, class ClsA {} class ClsB {} interface IntA {} interface IntB {} And I have 2 very similar methods: static T returnC() { // Here T extends the class return null; } static T…
Codebender
  • 14,221
  • 7
  • 48
  • 85
2
votes
3 answers

How was it decided that Predicate "and" method would have a consumer and not producer in java?

I was going through the Predicate class introduced in java 8 which is functional interface. There is a method and inside the Predicate class which is as following for composing multiple predicates into one. default Predicate and(Predicate
Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46
2
votes
1 answer

Types of argument to be passed to in Java

In Chapter 8 of Generic Types from Core Java Volume I Edition 10, NOTE: Another common use for supertype bounds is an argument type of a functional interface. For example, the Collection interface has a method default boolean…
Ray Jasson
  • 422
  • 2
  • 9
  • 23
1
vote
1 answer

Java possibly requiring unnecessary extends keyword in PECS construction

I have a code example where Java forces me to use the extends keyword on a class that is final in a PECS construction. I do not understand why exactly. The code can be found below or at https://onecompiler.com/java/3yvf4g97f. The compiler fails to…
meijuh
  • 1,067
  • 1
  • 9
  • 23
1
vote
1 answer

How to pass multiple Types that implement the same interface?

Firstly apologies about the not so great title, I am new to Java and wasn't sure how to title this. I have a interface class "TestInterface": ublic interface TestInterface { String getForename(); void setForename(String forename); …
1
vote
2 answers

How to allow a consumer of more precise type to be passed in as a consumer of a less precise type?

I have this following two functional interfaces: IndexBytePairConsumer.java package me.theeninja.nativearrays.core; @FunctionalInterface public interface IndexBytePairConsumer { void accept(long index, byte…
Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
1
vote
1 answer

Java preserve generic type in map of wildcard upper bound

Starting with these classes public class Event { private EventType eventType; public EventType getEventType() { return eventType; } public void setEventType(EventType eventType) { this.eventType = eventType; …
1
2