Get and Put principle
Get
when you want to restrict to add new entry into data structure then you should use extends with wildcard. Means when you only get values from the data structure then you should use extends wildcard.
Put
When you want to put values in data structure then you should use super with wildcard. Means you need to modify the data structure then you should go use the super with wildcard.
Look at the prototype of below method for understanding it better way.
public static <T> void copy(List<? super T> dest, List<? extends T> src){
for (int i=0; i<srcSize; i++){
dest.set(i, src.get(i));
}
}
Here we are taking values from src list and adding them in dest list.
If you will try to add in src list it will give you compile time error.
I have already explained why it will give error in this topic: Wildcards
when you want to restrict to add new entry into data structure then you should use extends with wildcard. Means when you only get values from the data structure then you should use extends wildcard.
Put
When you want to put values in data structure then you should use super with wildcard. Means you need to modify the data structure then you should go use the super with wildcard.
Look at the prototype of below method for understanding it better way.
public static <T> void copy(List<? super T> dest, List<? extends T> src){
for (int i=0; i<srcSize; i++){
dest.set(i, src.get(i));
}
}
Here we are taking values from src list and adding them in dest list.
If you will try to add in src list it will give you compile time error.
I have already explained why it will give error in this topic: Wildcards