This question already has answers here:
What is the difference between List and ArrayList? [duplicate]
(1 answer)
What does it mean to "program to an interface"?
(33 answers)
Closed 2 years ago.
So I started learning Java about a month ago. And there is this one thing that I can't quite put my finger on.
What is difference between
List<String> list1 = new ArrayList<>();
and
ArrayList<String> list2 = new ArrayList<>();
I get that in list1 declaration is done by using name of Array class and initialization by using name of ArrayList class, and in list2 declaration and intialization both are done by using name of ArrayList class. But what is the difference if there are any.
And even when I make object for inherited class and do the same thing, declaration using name of one class and initializing using name of other. Is there any difference apart form the fact that the code looks different??
Ps : Kinda new to all the terminology and learned most stuff from books, oracle website and stuff but not completely from one source so if i made any mistake please correct me :)
ArrayList is contained under the List interface. When you use ArrayList, you are adding more specification and using ArrayList methods.
See Polymorphism
Related
This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 2 years ago.
What is the difference between
List<String> lst = new LinkedList<>();
and
LinkedList<String> lst = new LinkedList<>();
There's no difference other than convenience - in the first case you can change LinkedList to other List implementation (such as ArrayList), if you find that it works better in your application, for example, without the need to change any other code.
This question already has answers here:
"Program to an interface". What does it mean? [duplicate]
(8 answers)
Closed 8 years ago.
In this statement List<String> MyList = new ArrayList<String>();
Why declare Mylist as a List of type String. just to make MyList a new instance of an ArrayList type String?
It seems unnecessary.
To paraphrase Item 52 of Joshua Bloch's Effective Java, using the interface instead of the class makes the surrounding code much more flexible. So if you wanted to change myList to something like a Vector, you wouldn't have to change the surrounding code much if at all. Bloch goes on to say that there are certain times, such as major updates to java, when other types of Lists are updated for effectiveness or new ones are added entirely.
Declaring MyList as type List<String> guarantees that you won't use methods only available for ArrayList<String>. That way, you can change the right-hand side of the declaration in the future.
List is just an Interface, not an implementation. There are many different types of lists, such as ArrayList and LinkedList.
So if you declare MyList an List<String> it could possibly be any type of List, as #espertus said, you can change what type of list MyList is in the future without having to change it's declaration.
This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 8 years ago.
Suppose we wanna define and use a LinkedList in our code.
I would define it this way:
LinkedList<String> list= new LinkedList<>();
Why some people use the interface List
List<String> list = new LinkedList<>();
to define a LinkedList? What are the advantages?
If you use following :
List<String> list = new LinkedList<>();
Then everywhere, in your code, you just using List<String> list. It means, that if you want use ArrayList, the only thing you have to do is to change one line : List<String> list = new ArrayList<>(); and it will work perfectly in all other code, no matter how complex it is.
If you are using LinkedList, you can have huge problems with switching to something else.
This question already has answers here:
Converting List<Integer> to List<String>
(21 answers)
Closed 9 years ago.
I've a list of user object. And I want to get list of user name except using loop.
Is there any collection or util function in java 7??
No, there's no util function. It's 3 lines of code to do it yourself.
Even if there was a util function, it wouldn't help with performance. It would be implemented as a for loop.
As for performance, have you actually profiled your application and seen that iterating the List is a performance hotspot?
I think what you mean is:
for (String pos: User) {
System.out.println(pos.toString());
}
This uses an Iterator, but since 1.5 Java you can use this shorthand to not show the Iterator.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the benefit of polymorphism using Collection interface to create ArrayList object?
what is the advantage of defining an Arraylist as
Collection<User> parameterGroupList = new ArrayList<User>() over ArrayList<User> parameterGroupList = new ArrayList<User>();
If the static type parameterGroupList is Collection<User>, the users of parameterGroupList will only use the operations available in Collection<User> and avoid the operations specific to ArrayList. As a result, it makes it easier for you to replace ArrayList to some other collection like LinkedList in future.
Simply putting same as the advantage of using Runtime Polymorphism.
What you are asking is Java 101 question.