Java Generics: List, List<Object>, List<?> -


Has anyone been explained, as much as possible, there is a difference between the following types?

  list list & lt; Object & gt; List & lt ;? & Gt;  

Let me make it more specific. I would like to use

  // 1 public zero CanYouGiveMeAnAnswer (list l) {} // 2 public zero CanYouGiveMeAnAnswer (list & lt; object & gt; l) {} // 3 Public Zero CanYouGiveMeAnAnswer (LIST & lt;? & Gt; L) {}  

That other posts have noted, you are asking about a Java feature named Generic, in C ++, this is called a template

Let me work in response to your questions. (If he does not have a naughty word for OO discussions).

Before Generic, you have good old solid vector classes like

  vector V = new vector ();  

You give vectors to any old object.

  V.add ("This is an element"); V.add (new integer (2)); V.add (new hashtable ());  

However, they do this by casting everything into one object (the root of all Java classes) and casting everything. Unless you try to recover the values ​​stored in your vector, it's okay. When you do, you have to put the value into the original class (if you want to do anything with it).

  string s = (string) v.get (0); Integer I = (integer) v.get (1); Hashtable H = (hashtable) v.get (2);  

Casting becomes very old, more than that, compiler wine on you about uncontrolled cast, for its a vivid example, using XML-RPC library from Apache (version 2 by the way) Please. The most important problem with this is that in order to properly put the consumers of your vector, you should know the correct class of values ​​on the compile time in such cases where the creators of the vector and consumer are one- They are completely different from the other, it can be a fatal issue.

Enter generics, generics attempt to create strongly typed classes for generic operations.

Arreelist & lt; String & gt; AList = new array list & lt; String & gt; (); AList.add ("A"); String element = aList.get (0); // no artist required System.out.println ("found one:" + element);

Now, if you take a look at the infamous gang of the design pattern book of four, you will see that some knowledge of divorcing variables from their implementation Is the class It would be better to think of contracts instead of implementation. Therefore, you can say that all the list items do the same thing: add () , get () , size () , etc. . There are several implementations of inventory operations that can choose to follow the contract in different ways (like ArrayList ). However, this object deal with the type of data is left to you as a runtime idea, the user of the generic class Put it together and you will see the code of the following line many times:

  list & lt; String & gt; L = New Arrestist & lt; String & gt; ();  

You should read that "L is a type of list that relates to string objects". When you start dealing with factory classes, it is important to deal with contracts rather than specific implementations. The factories produce different types of items in order.

Generic use is very easy (most of the time) However, on a horrible day you can decide that you want to apply the normal range. Perhaps you have thought of implementing a great new inventory when you define that class, then you can use & lt; as placeholder for that method. T & gt; will be tampered with. If you are confused, use the normal sections of the list until you are comfortable. Then, you can jump in the implementation with a little more confidence. Or you can see source code for different list sections which ship with JRE. Open source is great in this way.

Take a look at Oracle / Sun Cheers.


Comments