• Post Reply Bookmark Topic Watch Topic
  • New Topic

PECS rule generics?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys so I Understand what the pecs rule is when you want to add to a list use List<? super T> and when you want to read from a list use List<? extends T> but what I don't get is the following code,the code works fine but In the sortList method I am using extends Integer instead of super Integer,in the list I am calling the collections swap method on the list but technically I'm writing to the list because I am swapping the two values writing j to i and i to j,so how does this even work why am I allowed do this? I thought that you could only read from something when using extends in this case,

thanks

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you doubtless already know, the extends keyword in that context allows any type to extend itself, so Integer counts as a subtype of Integer for the purposes of generics. Rather like the way a set is usually a subset of itself: S₁ ⊆ S₂
Therefore Integer is included in <? extends Integer> Which is just as well because Integer is a final class which has not strict subtypes. But you are not sorting a List<? extends Anything>. You are sorting a List<?>. Not that you can use set() on a List<?> either. So let's have a look at the source code.
  • 1: Find the file in your Java® installation folder called src.zip.
  • 2: Unzip it into some convenient location.
  • 3: Navigate it via java→util→Collections and find that method.
  • Now tell me whether that is cheating? As for line 20, you might understand it better if you remind yourself of the return type of the set() method.
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for the reply Campbell that makes sense mainly about the Integer is a subclass of Integer in this case but the following code confuses me
    I am trying to call addToList() in sortList() add to List takes an argument of List<Integer> list,then in printList I use list which was passed in as the argument List<Integer> is a subtype of List<? extends Integer> yet this operation will not work I get an error when I try to call addToList() from sortList()


     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Adam Chalkley wrote:
    I am trying to call addToList() in sortList() add to List takes an argument of List<Integer> list,then in printList I use list which was passed in as the argument List<Integer> is a subtype of List<? extends Integer> yet this operation will not work I get an error when I try to call addToList() from sortList()


    A List<Integer> instance IS-A List<? extends Integer>, so the call to sortList() is allowed. However, a List<? extends Integer> instance is not guaranteed to be a IS-A List<Integer>, so hence, the call to addToList() is not allowed.

    As a counter argument, you can say that the Integer class is final, so hence, the only possible list for a List<? extends Integer> is a List<Integer> .... and ... you will be correct.  Unfortunately, the Java Language Specification does not mention this possible optimization / check, so, it still doesn't compile.

    Henry
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks Henry makes sense =)
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I fixed the code up a little but it doesn't do exactly what I want it to do when I add to the list in sortList with the addToList method it compiles fine as expected but it does not sort the newly added Integer that was was added in that method with the addToList method instead it sorts the list and puts the new Integer at the end no matter the number,

    I drew this out on a piece of paper a few times following the references but doesn't seem to make sense why the number won't be sorted as it is part of the list.



     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!