A Java ArrayList Qusetion - java

Question: If you have a List and it has String objects, which declaration(s) of your List does not require that objects retrieved using the get method be cast to Strings, before calling a String method? List <Object> a = new ArrayList <Object>();
I. List<Object> a = new ArrayList <Object>();
II. List<String> a = new ArrayList <String>();
III.List a = new ArrayList();
I don't really understand this question. I think you must cast to a String to use it as a String, then it must be a declaration that does not return a String object as a String.

Here's how you should think about the answer: What does each of the following return?
I. and III. are the same thing. Both instances will return a java.lang.Object if you call get. You'll have to cast that to a java.lang.String in order to use it.
Only II. will return a String if you call get, because of the generic declaration.

2 will not require explicit casting. 1 and 3 are effectively the same thing.

The second one.
ArrayList<String> a = new ArrayList <String>();
This means that the arraylist can only hold string type variables. It says string in the pointy brackets. This is called generics and it allows the data you take out to be of the form you specify, in this case a String.

I. and III both are a list of Objects (and I. has a typo on the declaration, the correct class type is "Object"). You can store anything in them, including Strings, but as far as the compiler can tell... they're of type Object, so if you want to call a String method on an object you retrieved with the ArrayList.get(...) method you will have to cast the returned object to String.
II. is a list of Strings (the type of stored objects is specified by the generics part between <...>), the compiler knows the ArrayList will only store Strings, so the ArrayList.get() will return a String directly, without the need for a cast.
You can see an example of this exact thing in the Java Generics Tutorial.

Related

<E> and <Object> differences and usage

Can someone explain me what are the differences by using E or Object for example in a class for Lists, and their singular usage and definition.
I have to use them in LinkedLists to implement methods.
Assuming that E comes from element, i would consider using generics in Collections when the method that compiles says that it took an array of a certain type, and returns an array of the same type.
On the other hand you can't mix oranges and apples. You would be able to add an Object to your String list if you could pass a string list to a method that expects object lists. (And not all objects are strings)
The syntax you've written as <E> refers to Generics.
Generics provide a way to re-use the same code generalized for different data types while still maintaining strict type checks at compile time. For example, as you mentioned, If you'd use a data structure handling elements of type Object, you wouldn't have type safety when adding or getting its elements because any class instance could be passed and retrieved from it. This would lead to error-prone code where elements retrieved from your data structure should be cast first to the expected data type in order to be used or in the worst scenario not being of the right type and knowing this only at run-time with no way to prevent it.
Because of this, Generics aid the developer allowing him/her to establish for each different situation the generic Type Parameter with a specific Type Argument (a non-primitive type). This not only provides much more flexibility and re-usability of your code, but once you've established that a generic Type Parameter E stands for a specific data type (Integer, String or other), then the compiler will make sure that every operation that your object will perform with the Type Parameter E will be compliant with the specific Type Argument you've put in place of E.
public class Test {
public static void main(String[] args) {
//Here I can add mixed non-primitive types because they're also instances of Object and therefor compliant to the expected type
List listObj = new ArrayList();
listObj.add(Integer.valueOf(5));
listObj.add("Hello");
listObj.add(Double.valueOf(3.77));
//Here I can't retrieve the elements with their right type even though I know I've placed an Integer, a String and a Double in its first, second and third position
Integer i1 = listObj.get(0);
String s1 = listObj.get(1);
Double d1 = listObj.get(2);
//Here I'm defining a generic list working only with Integer (for this specific instance)
List<Integer> listGenInt = new ArrayList<>();
listGenInt.add(Integer.valueOf(5));
//If I try to add any other type but Integer I'd get an error at compile time
listGenInt.add("Hello");
listGenInt.add(Double.valueOf(3.77));
//Here I can only expect to retrieve elements of the data type I've established for this object during its declaration (Integer)
Integer i2 = listGenInt.get(0);
//Here I'd get an error If I'd try read its elements with any data type but Integer
String s2 = listGenInt.get(1);
Double d2 = listGenInt.get(2);
//Here I'm defining a generic list working only with String (for this specific instance)
List<String> listGenStr = new ArrayList<>();
listGenStr.add("Hello");
//If I try to add any other type but String I'd get an error at compile time
listGenStr.add(Integer.valueOf(5));
listGenStr.add(Double.valueOf(3.77));
//Here I can only expect to retrieve elements of the data type I've established for this object during its declaration (String)
String s3 = listGenStr.get(1);
//Here I'd get an error If I'd try read its elements with any data type but String
Integer i3 = listGenStr.get(0);
Double d3 = listGenStr.get(2);
}
}
Here, there is also a link with examples and further explanations from the official tutorials by Oracle.
https://docs.oracle.com/javase/tutorial/extra/generics/intro.html

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).

What is the error Java ArrayLists

I am throughly convinced that this is snippet of code has an error. The Java from the ground up programming book says that there is no error. This is the code:
ArrayList temp = new ArrayList();
temp.add("35");
temp.add(35);
Isn't there an error with line 2? An arrayList cannot have a string and an integer within the same ArrayList correct? Please correct me if I am wrong, I have a test on this tomorrow. Thank you all in advance!
Yes, it is correct.
Since you haven't declared any type in ArrayList it allow all Objects.
Now your ArrayList as same as
ArrayList<Object> temp = new ArrayList<Object>();
And see below examples
ArrayList<String> temp = new ArrayList<String>();
temp.add("35");
temp.add(35); //compiler error, Only Strings please
see below example
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add("35");//compiler error, Only Integers please
temp.add(35);
And it's highly discouraged to use Raw types, Always prefer to use with specific type.
Now in first case ArrayList temp = new ArrayList();, when you return it returns an Object which is unkown type. It may be an Integer or a String, you need to check it manually after you got it back from List
ArrayList temp = new ArrayList(); There is no issue here. This is an ArrayList which will accept any data type since it is not defined specific data type (raw ArrayList).
it is correct because its a raw type means you can add any object.You can add anything that extends Object class
you can write temp.add(x); where x can be any thing String,int,long Date anything that extends Object
example
String x="";
int x1=1;
Date df=new Date();
temp.add(x);// will work
temp.add(x1);//will work
temp.add(df);// will work
yes your array list can hold both String and integer.
but array list only store objects String and Integer objects it doesnt support int primitive datatypes only objects inside arraylist
ArrayList<Object> arr=new ArrayList<>();
String s="35";
Integer in=35;
arr.add(in);
arr.add(s);`
It is correct. Since you haven't declare any type for list, it treats all the elements as Object class. Your definition is
ArrayList<Object> arr=new ArrayList<>();
for compiler. In this case 35 will be boxed to Integer and "35" will be a String. Both Integer and String are subclass of Object and there wan't be any compile time error.
In java there is something named as Generics which allows programmers to abstract over types.
Arraylist code internally uses it.So when you don't define any generic type the default type it takes is the Object class type.
And as String as well as Integer both are subclasses of Object class, so this operation is allowed according to OOPs.

ArrayList.toArray() method in Java

I am wondering why did they design the toArray method in ArrayList to take a input of an array in Java?
ArrayList<String> listArray = new ArrayList<String>();
listArray.add("Germany");
listArray.add("Holland");
listArray.add("Sweden");
String []strArray = new String[3];
String[] a = (String [])listArray.toArray(strArray);
To me it appears that, they dont need this input because the instance of the ArrayList itself has enough details to convert the data into an array.
My question is why do they still need the array to be passed in? Thanks.
Two reasons I can think of:
Erasure means that the generic parameters aren't available at runtime, so an ArrayList<String> doesn't know that it contains strings, it's just the raw type ArrayList. Thus all invocations of toArray() would have to return an Object[], which isn't strictly correct. You'd have to actually create a second array of String[] then iterate over the first, casting all of its parameters in turn to come out with the desired result type.
The way the method is defined means that you can pass in a reference to an existing array, and have this populated via the method. In some cases this is likely very convenient, rather than having a new array returned and then copying its values elsewhere.
In your code, the ArrayList can contain anything, not only Strings. You could rewrite the code to:
ArrayList<String> listArray = new ArrayList<String>();
listArray.add("Germany");
listArray.add("Holland");
listArray.add("Sweden");
String []strArray = new String[3];
String[] a = listArray.toArray(strArray);
However, in Java arrays contain their content type (String) at runtime, while generics are erased by the compiler, so there is still no way for the runtime system to know that it should create a String[] in your code.
You need it to get array types other than Object[]. listArray.toArray(new String[3]) shouldn't actually need a cast to String[], but it lets you write into arrays for specific types, rather than the (rather unsafe) approach of just casting Object[] to whatever your type is.
It is so that when you have an non-empty array you want object to be appended to from the arraylist conversion you can do that without having to create a new array, and do post processing of merging it, save resource and extra work.
Read the second paragraph of the javadoc: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray(T[])
ArrayList doesn't have enough information given that the "data" itself is stored in buckets of Object arrays. So, if you know "what" kind of items you have in your array list, you can achieve type safety by using this variant of the toArray method.
It is not compulsory to pass an Array as an argument to toArray(), you can directly call ArrayList.toArray()
As we know that Collection framework with generics works with the concept of Type Erasure. So the ArrayList becomes List of Object, so when you convert to array it will give an Object[] if you don't pass any argument.
Because the array into which the elements are to be copied should be specified to the JVM.
See this documentation

what is this mean in the ArrayList in Java

I'm new in Java, and I have seen a ArrayList example like this.
listing = new ArrayList<Lot>();
I know that if I want to create an empty array list. Then I will use ArrayList()
But I don't understand what is the <Lot> between the "ArrayList" and "()".
Can someone explain it to me?
Thanks
This is Java Generics. The <Lot> indicates that the ArrayList will contain only objects of type Lot. It is useful because the compiler can do type checking on your ArrayList.
It is called as type parameter. It denotes that ArrayList will only contain objects of type Lot
Check out concept of Generics.
You will get the use of this ArrayList<Lot> with this example :
// (a)Without Generics ....
List myIntList = new ArrayList(); // 1
myIntList.add(new Lot(0)); // 2
Lot x = (Lot) myIntList.iterator().next(); // 3
// (b)With Generics ....
List<Lot> myIntList = new ArrayList<Lot>(); // 1’
myIntList.add(new Lot(0)); // 2’
Lot x = myIntList.iterator().next(); // 3
Two points to be noted in the above e.g
In e.g(b), Since we already specified that ArrayList will contain only objects of type Lot, in Line 3, we didn't have to perform casting it to type object Lot. This is because the compiler already know that it will have only Lot type of objects.
Trying to add any other type of object to e.g (b) will result in compile time error. This is because the compiler has already identified this List is specific to contain elements of only type Lot. This is called type checking
It is an extension to Java's type system called, Generics.
Generics allow you to create a List that contains a specific sub-type of Objects (or a specific set of Objects that implement particular interfaces, instead of a collection that only holds plain Objects.
listing = new ArrayList<Lot>();
this line just says that the type of objects to be inserted,updated,retrieved in or from ArrayList are of the type Lot.
This is what is called the generics in java.
Using the generics type casting is not required at the time of retrieval of objects from any List.

Categories