Initialisation of Generic List of Generic List - java

Why can I not declare a List of Lists like this?
List<List<Object>> a = new ArrayList<ArrayList<Object>>();
An ArrayList is of type List so I presumed that the above code would be valid.
This is valid though:
List<ArrayList<Object>> b = new ArrayList<ArrayList<Object>>();
The inner list has to be declared as ArrayList.
Can someone explain this?

You're confusing your generics. The way to do what you want is
List<List<Object>> a = new ArrayList<List<Object>>();
By using ArrayList within the generic parameter, you're narrowing the scope of the object from List to ArrayList, thereby changing the generic signature. By using ArrayList outside the generic, you're still conformant; you're specifying which implementation of List you want to use, and that the objects it will hold are of type List<Object>, exactly as the left hand side states.
To see why this is important, consider this list:
// Invalid declaration, but pretend it is valid
List<List<Object>> a = new ArrayList<ArrayList<Object>>();
a.add(new ArrayList<Object>()); // valid
a.add(new LinkedList<Object>()); // not valid because it's not an ArrayList;
// but according to the left side, it is valid!
Of course, the best method is to use Java 7's diamond operator:
List<List<Object>> a = new ArrayList<>();

This is not as much a problem with the inner list as it is the problem of the generic parameter. As long as the generic parameter is the same (i.e. ArrayList<Object>) the initialization succeeds. Otherwise, Java does not let you make an assignment, because the compiler cannot guarantee type safety.
Imagine that this is possible:
// Let's pretend this compiles:
List<List<Object>> a = new ArrayList<ArrayList<Object>>();
Now the following must be valid:
a.add(new LinkedList<Object>());
However, this is invalid, because the object is an ArrayList of ArrayLists, so inserting a linked list into it is clearly invalid.
You can do this, however:
List<List<Object>> a = new ArrayList<List<Object>>();
This should be good enough to program to the interface.

Related

Typed vs. Non-typed ArrayList

What's the difference between using a typed vs. non-typedArrayList in Java?
For example, Using an ArrayList of CustomObject:
Typed:
ArrayList<CustomObject> typedArray = new ArrayList<>();
typedArray.add(new CustomObject);
or non-typed:
ArrayList<> nonTypedArray = new ArrayList<>();
nonTypedArray.add(new CustomObject);
Is there any situation where the latter is preferred? Is there any difference when the ArrayList is holding different datatypes, e.g. an ArrayList of String, Int, etc.?
In the Second Approach, it is not mandatory to add only CustomObject whereas it is in 1st Approach, otherwise, you will get Compilation Error.
ArrayList<CustomObject> typedArray = new ArrayList<>();
typedArray.add(new CustomObject());
This approach is generally preferable as there are no chances of having Class Cast Exception but in second approach there are high chances of that !!
JavaDocs explains it beautifully : Why to prefer Generics
Stronger type checks at compile time.
Elimination of casts.
Enabling programmers to implement generic algorithms.
It's never preferable to use the latter option. I don't think that is even possible. I think you meant:
ArrayList nonTypedArray = new ArrayList();
This syntax is left over from Java 1.4 and earlier. It still compiles for the purposes of backwards compatibility.
Generics was introduced in Java 1.5 which allowed you to specify the types between angled brackets.
It is always preferable to use generics because it is more type-safe.
That is, if you specify
ArrayList<String> typedArray = new ArrayList<String>();
Then you cannot accidentally add an integer to this array list; if you tried to add an integer, the program would not compile.
Of course, Generics ensures type safety at compile time. At runtime ArrayList<String> typedArray = new ArrayList<String>(); becomes ArrayList typedArray = new ArrayList();. This is to maintain backwards compatibility.
What's the difference between using a typed vs. non-typed ArrayList in
Java?
A typed/generic ArrayList is a collection of objects in which the "type" of the object is defined in angled brackets. Generics were introduced in Java 5 to create type-safe collections.
Before Generics the collection was called untyped/raw type collection because there was no way to specify the compiler the type of the collection being created.
The difference between both is to detect type-safe operations at compile time.
In both of your cases, you are adding object(s) of type 'CustomObject' to the ArrayList. There will be no issue while adding elements in the list, as both lists will consider them as typed objects.
Typed:
ArrayList<CustomObject> typedArray = new ArrayList<CustomObject>();
typedArray.add(new CustomObject);
Untyped:
ArrayList<> nonTypedArray = new ArrayList<>();
nonTypedArray.add(new CustomObject);
Is there any situation where the latter is preferred?
I don't think so. As generics are recommended to be used while creating a list to ensure type-safe operations.
Is there any difference when the ArrayList is holding different
datatypes, e.g. an ArrayList of String, Int, etc.?
Surely, there is a reasonable difference. For an untyped list, you will need to add type-cast while fetching elements from a list. As there is a possibility of the compiler throwing a ClassCastException at runtime due to different types of elements.
In runtime, there is absolutely no difference, however in compilation time, using type parameters can save you from a plethora of errors, so it is always preferable to use generics properly.
The only case where raw types are used reasonably is in legacy applications, but even in this case, you try to use typed parameters if you can.
The use of type simplifies your coding removing the need of casting and also stores your data efficiently
https://docs.oracle.com/javase/tutorial/java/generics/why.html
Yeah, I know this is an old post. But I wanted to share an instance where an untyped ArrayList is useful: when you're writing a function that supposed to act on arbitrary element types. For example, suppose you want to make a generic shuffle function that knows how to shuffle an array. Like so:
ArrayList<Die> diceRolls = getGetPossibleDiceRolls();
ArrayList<Card> cardDeck = getPossibleCards();
ArrayList<GirlToDate> blackbook = getBlackbook();
shuffle(diceRolls);
shuffle(cardDeck);
shuffle(blackbook);
.
.
void shuffle(ArrayList array) {
int size = array.size();
for (int i=0; i<size; ++i) {
int r = random.nextInt(size - i) + i;
// Swap
Object t = array.get(i);
array.set(i, array.get(r));
array.set(r, t);
}
}
Some might argue "yeah, but the proper way to do this is to create an interface or subclass of something like a Shuffleable type..." But really?
In Java 1.7 and upwards you should normally use the constructor like this:
ArrayList<MyObject> list = new ArrayList<>();
or else for a more general List object:
List<MyObject> list = new ArrayList<>();
Observe that you only specify the type <MyObject> once, not twice. This makes your code easier to maintain. The <> causes the constructor to return an ArrayList which is already typed to match the field/variable to which it is being assigned - so that no cast will be required in the calling code.
Do not use new ArrayList() as the constructor. This returns an untyped ArrayList which then has to be cast to a type to match the field/variable to which it is being assigned. This means unnecessary type checking and casting and so generally reduces performance.

ArrayList Generic without Type

recently I read a piece of code which seems weird to me. As we know, we need to initialize the generic type in collections when we need to use them. Also, we know Collections can contain Collections as their elements.
The code:
public class Solution {
public static void main(String args[]) {
ArrayList res = returnlist();
System.out.print(res.get(0));
}
public static ArrayList<ArrayList<Integer>> returnlist() {
ArrayList result = new ArrayList();
ArrayList<Integer> content = new ArrayList<Integer>();
content.add(1);
result.add(content);
return result;
}}
My question is
why can we use ArrayList result = new ArrayList(); to create an object, since we have not gave the collection the actual type of element.
why can we use result.add(content); to add a collection to a collection with collection "result" is just a plain collection. We have not defined it as a ArrayList of ArrayList
Java generic collections are not stored with a type to ensure backwards compatibility with pre J2SE 5.0. Type information is removed when added to a generic collection. This is called Type Erasure.
This means that a generic collection can be assigned to a non generic reference and objects in a generic typed collection can be placed in an non generic, nontyped collection.
All Java generics really does is make sure you can't add the wrong type to a generic list and saves you from doing an explicit cast on retrieval; even though it is still done implicitly.
Further to this
the Java section of this answer goes a little deeper into what I just said
this article also covers basically what you were asking in a more complete way
other things to watch out for with Type Erasure
Just adding up to provide summarized answer
Old way :
(A) ArrayList result = new ArrayList();
will create an Arraylist to hold "Object"
New Way :
ArrayList<Integer> content = new ArrayList<Integer>();
this represents an Arraylist which will hold "Integer" objects. This was introduced for compile-time type check purposes.
why ?
Consider the first case. Its input type is Object. We know that Object is the super class of all classes. We can pass in an Integer object, String object and so on. When fetching the data the developer has to perform proper type casting. Say if the developer initially thinks the code will accept Integer objects so he adds the following typecast while fetching the data
Integer integer=(Integer) content.get(0);
This is supposed to work. But if mistakenly he passes a String it will result in run-time error.
How it can be avoided ?
By introducing compile time checks
How it works ?
when we specify parameterized type only Integer objects can be added to the ArrayList collection. Else it will show error.
content.add(3); // will work
content.add("HARSHAD"); // error shown
If parameterized generic types are for type checking purposes how correct data can be retrieved from the list ?
The compiler implicitly performs type conversion. See the sample code
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
Integer integer=list.get(0);
System.out.println(integer);
What the compiler actually does when you perform compilation ?
//perform type erasure
(B) List list=new ArrayList();
list.add(1);
list.add(2);
// the arraylist inturn accepts Object when you run the code
//add casting
Integer integer=(Integer)list.get(0);
Conclusion
If you see the codes (A) and (B) both are the same. Only difference is that in the second case the compiler implicitly does the same operation.
Finally to answer your question ...
ArrayList result = new ArrayList();
is allowed for backward compatibility purposes. Although this is not recommended.
Official link from Oracle docs explaining the same concept.
Generics were added to Java only in Java 5. Before that, when you use a collection, it always meant collection of objects. The old syntax is left as is for backward compatibility. So ArrayList result = new ArrayList() is actually creating an ArrayList<Object>. Since ArrayList is also an object, you can add content to the variable result.
why can we use ArrayList result = new ArrayList(); to create an object, since we have not give the collection the actual type of element.
Because java wants to it backward compatible. Generics is more of compiler feature for ensure type safety, collections can store any type of object at runtime.
Java compiler will not give you compiler error for this but it must have given you compiler warning that it is unsafe to use generic classes without type.
It may be a remnant from before generics came along to java (Java 4 or 5 I think).

difference between List and List<?> [duplicate]

This question already has answers here:
What's the difference between unbounded wildcard type List<?> and raw type List?
(4 answers)
Closed 4 years ago.
I've read alot about this, and I know that:
List<Object> listOfObject = new ArrayList<TYPE>(); // (0)
//can only work for TYPE == Object.
//if TYPE extends Object (and thus objects of type TYPE are Objects),
//this is not the same with Lists: List<Type> is not a List<Object>
Now I've read that the following is ok:
List undefinedList = new ArrayList<TYPE>(); // (1)
//works for ANY type (except for primitives)
And
List<?> wildcardList = new ArrayList<TYPE>(); // (2)
//also works for ANY type (except for primitives)
Then:
List undefinedlist = new ArrayList(); //no TYPE specified
undefinedList.add(new Integer(1)); //WORKS
undefinedList.add(new String("string")); //WORKS
However:
List<?> wildcardList = new ArrayList<TYPE>(); //TYPE specified
wildcardList.add(new TYPE(...)); //COMPILER ERROR
example:
List<?> wildcardList = new ArrayList<String>(); //TYPE specified
wildcardList.add(new String("string")); //COMPILER ERROR: The method add(capture#1-of ?) in the type List<capture#1-of ?> is not applicable for the arguments (String)
I do understand why you can't add anything to the wildcardList, since its type can be anything. However, why can you add to the undefinedList??
They seem the same & show the same behavior, given (1) and (2).
List undefinedList and List<?> wildcardList are not the same, as you discovered yourself. The first is raw type and the second is unbounded wildcard.
Use the unbounded wildcard if you want to use a generic type but you don’t know or care what the actual type parameter is. You cannot put anything (except null) into this list, and all you know about the element you get out of it is that they extend Object (actually List<?> is the same as List<? extends Object>). Unbounded wildcards are useful, because if you would declare something naively as List<Object>, you could not assign for example List<String> to it, while you can assign a List<String> to a List<?>
You should (almost) never have the need to use raw types, they are available only for compatibility with code written before Java 5.
List<?> is read as a list of some unknown type . As a programmer you can not make any assumption of what type that is and you can not put anything into such a collection other than null . But you can be rest assured that your list is type safe since the compiler will guarantee type safety for you .
List is basically called raw type . That is to say that it has opted out of type safety guaranteed by the compiler . So you can put elements of any type into that List destroying its invariants . Don't code with raw types any more . They are basically supported for backward compatibility because java was already in the second decade of development when Sun brought generics to the table and a awful lot of code was written using raw types and those programs would otherwise break.
List means that this is a list of unknown type - as such you wouldnt use it at creation time (as in your example), you'd typically use it as a method parameter. Unbound wildcards are only really useful when used as parameters in methods, such as:
public void printList(List<?> items)
This could iterate of a list of (any) unknown items. In this case List items would achieve the same purpose, but client would probably get a warning.
If you had the following:
public void printList(List<Object> items)
Then only a list of Object could be processed - not a list Strings, Integers etc. Only Objects.
Take a look at Unbounded Wildcards - it explains its pretty well
The "undefined" List contain list of type Object which is the father of all types and hence the List is not type-safe(is interconvertible)
which is why this:
List undefinedlist = new ArrayList<TYPE>(); //IS LIST OF OBJECTS
undefinedList.add(new Integer(1)); //WORKS
undefinedList.add(new String("string")); //WORKS
well.. works!
Basically, ? in the following
List<?> wildcardList = new ArrayList<TYPE>();
means some unknown (particular) type. So, it doesn't allow you add something like String, or Integer to a list of some unknown type, because generics is meant to be type-safe.
While, in the following
List<Object> listOfObject = new ArrayList<TYPE>();
you can add anything to it because everything is an Object in Java. So it's type-safe.
And also with the following
List undefinedList = new ArrayList<TYPE>();
you are telling the compiler that you don't want to use generics there. Which means every method you invoke on undefinedList will be non-generic since you have decided to use the raw List. Non-generic versions of all the containers in the collection framework were written to work for Object (which any object in Java is).
The List<?> type is generic: whatever type you put in place of the question mark will be used in the methods of the list. So you can do list.add(item) and it will only allow you to put in a String if you created a List<String>. Type-safety first.
List<String> list = new List<String>();
list.add("A"); // <-- Correct
list.add((Integer)10); // <-- Error, it is a List of Strings
The List on the other hand allows any Object to be put in there. So you can make a List, put a Giraffe in there, and later a Squid. It does not care, and could be a source of programming errors if you expect only Giraffe objects to be in there.
List list = new List();
list.add("A"); // <-- Allowed
list.add((Integer)10); // <-- Also allowed,
// but now the list contains not only strings

Why are these invalid in Java?

List<Object> myList = new ArrayList<String>(); //(hint: no)
Map<Integer> myMap = new HashMap<int>(); // (hint: also no)
Why are the statements wrong in the above declarations?
Let's look at the first example. Think of the operations you should be able to do on a List<Object>: Add, Remove, Retrieve any Object.
You're trying to fill those requirements with a collection that you can Add, Remove, and Retrieve only strings.
List<Object> myList = new ArrayList<String>();
// This should be valid based on the List<Object> interface but obviously
// MyClass isn't String...so what would the statement do?
myList.add(new MyClass());
The second is simply because Generics in Java don't support primitive types. For more information, you could check out:
java - Why don't Generics support primitive types?
For the first one, because Java generics are not covariant. So you can't even do:
ArrayList<Object> myList = new ArrayList<String>();
For more info, see this article: Java theory and practice: Generics gotchas.
For the second one, you cannot use primitives as the generic type.
1) What would happen if anyone added a Long to the list that is referenced by myList?
2) You can't have a primitive there.
The first is impossible. You can use a wildcard:
List<?> myList = new ArrayList<String>();
But now, your list is completely unusable. Adding, removing, etc doesn't compile anymore:
myList.add(new Object()); // Error
myList.add("error"); // Error
You can't use primitives (int, double, ....) with generics. You have to use the wrapper class:
HashMap<Integer> myHap = new HashMap<Integer>();
for the second example Map has 2 generic parameters a Key and Value but the following still wont work
Map<Integer,Object> myMap = new HashMap<int,Object>();
that because as said before primitive types don't work with java generics (besides generic types always derive from Object and type erasure translates all generic variables to type Object)
For all things Java-Generics, refer to Angelika Langer. In this case, Is List<Object> a supertype of List<String>?
Even though Java does support auto-boxing, it doesn't automatically convert <int> to <Integer>. IMO this is partly to remind users that you can get nulls.

Type mismatch error

I am trying to create a list of list of strings,
List<List<String>> string= new ArrayList<ArrayList<String>>();
and getting Type Mismatch error:cannot convert from ArrayList<ArrayList<String>> to List<List<String>>.
I know I can change
List<List<String>> to ArrayList<ArrayList<String>>
and it will work fine.
But I was just wondering why doesn't it let it happen? Simple List<Object> can refer to ArrayList<Object> so what is different about this?
Because a List<List<String>> would accept other lists of strings that were not array lists, which is not how you declared the type initially. For example, if you converted to a List<List<String>>, you would technically be allowed to add a LinkedList<String> to it! And that's not an ArrayList<String>. Allowing that cast would effectively break the generic type system.
In other words, you could convert to List<ArrayList<String>>. But not List<List<String>>.
List<List<String>> string = new ArrayList<List<String>>()
Would be the right way of do it.
Regards,
You cannot assign ArrayList<ArrayList<String>> to List<List<String>> because it violates the compile time type-safety promise of generics. If this assignment were valid, then we can insert LinkedList<String> into ArrayList<ArrayList<String>> and subvert the type-safety.
The canonical example of this is List<Number> is not List<Integer>. It can be shown easily by contradiction.
List<Integer> ints = new ArrayList<Integer>();
ints.add(42);
//This results in compiler error
/* List<Number> nums = ints; */
//If it were not and an error, then you could
List<Number> nums = ints;
//Now you can insert doubles into a list of `Number`s
nums.add(0.1);
//This would allow 0.1 to assign to an int
Integer x=ints.get(1);
//This will cause ClassCastException and
//violate type-safety provided by generics.
In addition to what guys have already said I'd like to notice that definition you wrote:
ArrayList>()
works for you but is not good enough.
You should say:
List> list = new ArrayList>();
The rule is never write concrete class into generics definition and in left part of assignment if there is an abstract class or interface. My version allows changing the list implementation in future without any changes in code that use this data structure.
Moreover List is a Collection. If it does not matter for you to use specific list's features (e.g. if you are going to only iterate over this list) you can say the following:
Collection> list = new ArrayList>();
In this case in future you even can change List to Set:
Collection> list = new LinkedHashSet>();

Categories