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.
Related
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
This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 2 years ago.
What's the difference between the following lines:
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList();
ArrayList<String> list = new ArrayList<String>();
I just started learning about collections and data structures from the HeadFirstJava book and am a bit confused since I see people use all three of the examples above while researching.
The first and second line use raw types.
A raw type is a type that is used without type parameters, even though the base class has a type argument.
Raw types exist only for backwards compatibility with ancient (pre-Java 5) code and should never be used in new code at all. The rules about raw types are weird and unintuitive.
The third one is correct, but can be written in a shorter way like this:
ArrayList<String> list = new ArrayList<>();
This one will let the compiler "guess" what is meant to go into the <>.
This question already has answers here:
Polymorphism: Why use "List list = new ArrayList" instead of "ArrayList list = new ArrayList"? [duplicate]
(8 answers)
Closed 5 years ago.
Which one is better and why ?
a) List<String> list = new ArrayList<>();
b) ArrayList<String> list = new ArrayList<>();
It depends on the context.
If you care only about the list semantics and not for the particular implementation, go for List<String>. If you want to enforce a particular implementation, go for ArrayList<String>, LinkedList<String> or anything else.
In most cases, you will want to go on with the interface and not the particular implementation.
(a) is better because it is more flexible. If for example, suddenly your requirement changes and says "Instead of creating a new empty array list to assign to the variable list, we will populate the list with items filtered from another list", you need to change your code for that. You might change it to something like this:
List<String> list = anotherList.stream().filter(x -> x.length() > 2).collect(Collectors.toList());
Since collect(Collectors.toList()) returns a List, you also need to change the type of list if you were using the code in (b).
Another situation is that later on you found out that you need to assign some other kind of list e.g. LinkedList to list. If you were using the code in (a), you can just assign the linked list straight away. If you were using the code in (b), you will have to go back to the declaration of list and change the type.
The general pattern is that as you use more "abstract" types, your code becomes more flexible but at the same time you can do less with your variables. For example, you can't use methods declared only in ArrayList on your list variable, but on the other hand, other kinds of lists can be assigned to list. If you an even more abstract type, Collection, then you can't use the members only declared in List anymore, but then all kinds of collections - Set, Map etc - can be assigned.
This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 8 years ago.
When do I use List and when do I use ArrayList in Java? Please phrase in terms of practical situations where you would rather apply one over another. Thank you!
Edit : Also, LinkedList. Business situations where these are used, thanks, thats what's different about this question.
List is an interface. The other two are implementations of which.
You mostly want to code against interfaces. That is you wil do something like
List<String> strList = new ArrayList<String>();
Later on in the coding process, you may find that LinkedList has better performance for your scenario, so you just need to change one single place. Or maybe you don't care which concrete implementation is used, you just need "some sort of list". Then you could use an injected List implementation. Like this:
class ExampleClass{
private List<String> strList = null;
// We don't know and we don't care if Array or Linked List.
public ExampleClass( List<String> aList ){
strList = aList;
}
//...
}
For the differences between the implementations, see the links given in the comments as "possible duplicate of ..." or the JavaDoc.
***There's no difference between list implementations in both of your
examples. There's however a difference in a way you can further use
variable myList in your code.
When you define your list as:
List myList = new ArrayList(); you can only call methods and reference
members that belong to List class. If you define it as:
ArrayList myList = new ArrayList(); you'll be able to invoke ArrayList
specific methods and use ArrayList specific members in addition to
those inherited from List.
Nevertheless, when you call a method of a List class in the first
example, which was overridden in ArrayList, then method from ArrayList
will be called not the one in the List.
That's called polymorphism. You can read upon it.***
This answer was given by ATrubka here
This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 9 years ago.
Hi,I am a beginner of Java, I was taught to use "ArrayList" in OO programming in the Java lecture, however, I came across "List" today and have no idea how to use it, so what the difference between ArrayList and List? And what the same attributes of them?
something like:
List<...>list=new List<...>()
ArrayList<...>list=new ArrayList<...>()
List is an interface, whereas ArrayList is a concrete class that implements that interface
List is an interface.
ArrayList is a class that implements List.
You can't instantiate an interface, you have to instantiate one of classes which implements it.
List defines the basic contract that is expected that implementations would provide. ArrayList is a implementation of this contract that is backed by a dynamic array.
The great thing about this idea is, if you have a method that needs access to some kind of List, you can simply ask that callers pass you any implementation of List, meaning you don't need to know or care how the List is actually implemented, only that it will provide the contract described by List
You can't create an instance of List directly, you need to use one of the implementations, like ArrayList or LinkedList...
For example...
List<String> listOfStrings = new ArrayList<String>(25);
List<String> anotherListOfStrings = new LinkedList<String>();
List is an interface, essentially providing a list of operations (add, remove, get...), but no implementation (you cannot do new List). There are several classes implementing List interface, including ArrayList (using, as said, an array as internal container) and, for instance, LinkedList. You can instantiate these, instead, and write:
List<ElementType> myList = new ArrayList<ElementType>();
Using List as a type for myList reduces the effort if you want to replace ArrayList with LinkedList:
List<ElementType> myList = new LinkedList<ElementType>();
(immagine, instead, if you had to replace ArrayList with LinkedList in several places, instead). Additionally, you will hide the actual implementation lying behind, so that other programmers don't make tricky assumptions on how the List might behave.