• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections framework question  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 465
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys have a question

so here is a method from the collections framework


public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

now this will work if I pass a list<? extends T> into the method

the question is why and how is this allowed Collection<Integer> isn't the same as List<Integer>

thanks
Staff note (Bear Bibeault):

More advanced than "beginning"; moved to Java in General.

 
Henry Wong
author
Sheriff
Posts: 23280
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:
the question is why and how is this allowed Collection<Integer> isn't the same as List<Integer>


For this method call to be allowed, a List<Integer> must be IS-A Collection<Integer>, and that is exactly what it is.

Henry
 
Zachary Griggs
Ranch Hand
Posts: 82
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

List implements Collection. In other words, a List is a more specific type of Collection. Therefore anything that works on a Collection will also work on a List.

This is also the reason why you should declare your variables and methods to take the most general type of object as you can and still have the method function as intended. For example, let's say we declare this function

This is almost always poor practice. Why? Because a LinkedList behaves the same as an ArrayList, so why shouldn't it work in this method? Instead:

Or - if you don't need the operations specific to List, you could declare it a Collection. And an ArrayList passed in would still work, but now the code is far more flexible. If later on, you decide that a different data structure would be preferable, you only need change the declaration of your List. You don't need to go through and change all the different methods that do operations on it.

Also consider the following code:


Likewise, this is usually poor practice. Why do you need an ArrayList declared? You can declare it:


In the same way, when you create an interface and have multiple objects implementing it, you can declare a reference variable of that interface type instead of the concrete implementation. Then you can swap out implementation details however you wish, and know that it will still fulfill what you need.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:. . . List implements Collection.  . . .
You mean that List extends Collection, surely? So, yes, as you said, a List is a more specific type of Collection.
 
Zachary Griggs
Ranch Hand
Posts: 82
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. I always forget that interfaces extend interfaces instead of implementing them.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!