File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes use of List<?> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "use of List<?>" Watch "use of List<?>" New topic
Author

use of List<?>

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Hi Friends,

I am really confused with the use of List<?>....

Like
List<?> lst=new ArrayList<Integer>();

I know i cannt add anything other than null in this..But can anybody know the use of this...Why we need this..Please provide me one example for its usage..


Thanks
Shanky


SCJP6.0,My blog Ranchers from Delhi
Tomasz Sochanski
Ranch Hand

Joined: Jan 13, 2009
Posts: 47

Consider following method:


Using such method you can get last element of any List regardless it is List<Integer> or List<Anything>. Note, that List<?> is not equivalent to List<Object>. If you would try to replace method signature in line 01. to

then you can use this method only for List<Object>, but not for List<Integer> or List<Anything>. This is because List<Integer> or List<Anything> are not subtypes of List<Object>.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Further to what Tomasz posted, you can also use ? like this:



if you wanted the list type to be subtypes of SomeClass.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Shanky Sohar wrote:

I am really confused with the use of List<?>....



The Need for Generics says -


The motivation for adding generics to the Java programming language stems from the lack of information about a collection's element type, the need for developers to keep track of what type of elements collections contain, and the need for casts all over the place. Using generics, a collection is no longer treated as a list of Object references, but you would be able to differentiate between a collection of references to Integers and collection of references to Bytes. A collection with a generic type has a type parameter that specifies the element type to be stored in the collection.

As an example, consider the following segment of code that creates a linked list and adds an element to the list:

LinkedList list = new LinkedList();
list.add(new Integer(1));
Integer num = (Integer) list.get(0);


As you can see, when an element is extracted from the list it must be cast. The casting is safe as it will be checked at runtime, but if you cast to a type that is different from, and not a supertype of, the extracted type then a runtime exception, ClassCastException will be thrown.

Using generic types, the previous segment of code can be written as follows:

LinkedList<Integer> list = new LinkedList<Integer>();
list.add(new Integer(1));
Integer num = list.get(0);


Here we say that LinkedList is a generic class that takes a type parameter, Integer in this case.





William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Dan Drillich wrote:
Shanky Sohar wrote:

I am really confused with the use of List<?>....



The Need for Generics says -


The motivation for adding generics to the Java programming language stems from the lack of information about a collection's element type, the need for developers to keep track of what type of elements collections contain, and the need for casts all over the place. Using generics, a collection is no longer treated as a list of Object references, but you would be able to differentiate between a collection of references to Integers and collection of references to Bytes. A collection with a generic type has a type parameter that specifies the element type to be stored in the collection.

As an example, consider the following segment of code that creates a linked list and adds an element to the list:

LinkedList list = new LinkedList();
list.add(new Integer(1));
Integer num = (Integer) list.get(0);


As you can see, when an element is extracted from the list it must be cast. The casting is safe as it will be checked at runtime, but if you cast to a type that is different from, and not a supertype of, the extracted type then a runtime exception, ClassCastException will be thrown.

Using generic types, the previous segment of code can be written as follows:

LinkedList<Integer> list = new LinkedList<Integer>();
list.add(new Integer(1));
Integer num = list.get(0);


Here we say that LinkedList is a generic class that takes a type parameter, Integer in this case.






Completely out of my question....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use of List<?>