Java generics PECS

java generics super
java 8 generics
java generics medium
java generic method parameter extends
java generics wildcard
java generic list
java wildcard vs generic
generics in java howtodoinjava

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<T> {

    //List of branches for this tree
    private List<Tree<? super T>> branch = new ArrayList<Tree<? super T>>();
    public Tree(T t){ this.t = t; }
    public void addBranch(Tree< ? super T> src){ branch.add(src); }
    public Tree<? extends T> getBranch(int branchNum){
        return (Tree<? extends T>) branch.get(branchNum);
    private T t;

A nice mnemonic you can use is to imagine returns for extends and accepts for super.

So a Tree<? extends T> reads Tree<? returns T>, which means that you can call the methods in Tree that return T, but not the methods that accept T as an argument type.

What is PECS (Producer Extends Consumer Super)?, tl;dr: "PECS" is from the collection's point of view. If you are only pulling items from a generic collection, it is a producer and you should use extends ; if you are only stuffing items in, it is a consumer and you should use super . If you do both with the same collection, you shouldn't use either extends or super . Java Generics PECS – Producer Extends Consumer Super By Lokesh Gupta | Filed Under: Java Generics Yesterday, I was going through some java collection APIs and I found two methods primarily used for adding elements into a collection.

Java: Producer Extends, Consumer Super, Java generics is all about providing type safety. this is where the title of the article comes (Producer Extends, Consumer Super), or PECS. By adopting the PECS principle, the method/API allows its parameter being of more types. An example of PECS adoption can be found in java.util.List interface. boolean addAll(Collection<? extends E> c) is an example of “PE”. We can image that addAll is implemented as below.

The following cleared things up a bit in my mind. Hope it helps you.

Imagine a list of elements of some unknown type X and there is some logic some where that scans the list as elements of type X explicitly (after the list is built). Lets call this logic Logic1.

If you are going to scan that list as objects of type T, there is a requirement that X be derived from T in some form. Otherwise, you will have problems. Hence your signature should be:

void scan(List<? extends T>);

If you are going to add objects of type T to the list, there is a requirement that T be derived from X. Otherwise Logic1 mentioned above will have problems. Hence your signature should be:

void add(List<? super T>);

Note that it is my understanding that both "scan" and "add" can actually remove entries from the list. I find PECS to be a little confusing. The "producer" and "consumer" comes from the perspective of the list. I don't like to look at list as active thing that can execute logic. I like SEAS (Scan Extends and Add Supers). Just my 2 cents.

Time To Really Learn Generics: A Java 8 Perspective, The term PECS stands for "Producer Extends, Consumer Super", which is an odd acronym coined by Joshua Block in his Effective Java book but  PECS (short for "Producer extends and Consumer super ") can be explained by : Get and Put Principle Get And Put Principle (From Java Generics and Collections)

Java Generics PECS & Get-Put , Java Generics PECS & Get-Put Principle, Non-Reifiable Types & Erasure, Covariance & Contravariance. Joshua Bloch's in the book Effective  A brief example of PECS in Java generics - Producer extends, Consumer super //numberList is a producer of subtypes of numbers. Hence is generified with ? extends Number.

Java Generics Cheat Sheet, In this article, we'll give some background on Java Generics, look at wildcards, methods, and PECs. Then, at the end of the article, we'll share  This is what Maurice Naftalin calls The Get and Put Principle in his Java Generics and Collections and what Joshua Bloch calls The PECS Rule in his Effective Java. Bloch's mnemonic, PECS, comes from " Producer Extends, Consumer Super " and is probably easier to remember and use.

Java Generics Tutorial - Part III - Wildcards, The previous posts introduced us to the basics of Java generics y their Bloch's mnemonic, PECS, comes from "Producer Extends, Consumer  What Are Java Generics? Java Generics allow developers to implement a collection of types in a single declaration, reducing the verbosity of your code as well as its cost of maintenance. Java Generics were added to the now ancient Java 5 way back in 2005 when the world was a much simpler place.