r/javahelp • u/synopticaldad • Jun 11 '20
what is the difference between ArrayList amd List?
i was going through collections in java and i implemented the ArrayList in the following ways:
List<Integer> list1= new ArrayList<Integer>(); ArrayList<Integer> list2= new ArrayList<Integer>();
I know that by creating the reference of List, I can access all the methods of List class but I don't feel any advantage of it.
4
u/morhp Professional Developer Jun 11 '20
In your code it doesn't make a huge difference. But essentially, an ArrayList is-a List, but there are other types of Lists in java.
For example there is a method List.of(1,2,42) in newer java versions that just returns an unmodifiable list of the specified items. But that that's not an ArrayList.
So if you always write List, you can use multiple types of list without changing your variable types and that's generally recommended and less to type.
2
Jun 11 '20
Basically you use a reference for the interface List because in case you decide to change the implementation ArrayList to some other List implementation you won’t have to change any of the methods you used. It guarantees that you don’t use Implementation specific methods so you’re free to use whatever implementation later on with minimal changes to code.
-4
Jun 11 '20
Now is the perfect time to read the documentation for List and ArrayList. One is an interface.
Google "why write to an interface..."
3
u/synopticaldad Jun 11 '20
too beginner question to ask.. 😅
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Jun 11 '20
Your question is perfectly fine. That user barely knows what they're talking about, puts in no effort in answers and should not be posting here.
1
u/synopticaldad Jun 11 '20
i love the feedback of this community. he said what a normal user does, no doubts. ✌
1
u/Yithar Intermediate Brewer Jun 11 '20
I would ignore that person. They seem to like acting superior than others but they themselves have a lot to learn:
-3
Jun 11 '20
It's not the nature of the question but sometimes some minimal effort of reading Java's official documentation might be the only thing you need to do.
10
u/ryosen Extreme Brewer Jun 11 '20
Let's leave the "just fucking google it" answers for Stack Overflow, shall we? Polymorphism is an advanced topic and not something covered at the beginning level that this sub is intended to support.
50
u/ryosen Extreme Brewer Jun 11 '20 edited Jun 11 '20
Listis an interface.ArrayListis a concrete implementation of that interface.An interface is a design contract. At its most basic level, an interface is an agreement that any class that claims to implement that interface will, at a minimum, provide the functionality described by that interface in the same way.
Listdescribes a basic set of common methods that concrete classes implementing theListinterface need to provide. Some examples of this are theadd(),remove(),size(), anditerator()methods. Any class that implements the design specified by theListinterface provides these methods.You can't instantiate an object of type List. That doesn't actually exist. Instead, you have to use an object that is designed in a way that has agreed to do it the way that the
Listinterface specifies. One example of this isArrayList, which is the most common concrete implementation of theListinterface.The ArrayList class provides all of the methods and functionality required by the List interface. It also provides additional functionality that support the type of implementation that ArrayList is. An example of this is
ensureCapacity(). This method is helpful to make sure that the ArrayList will have enough space for what you want to put into it. It's a nice feature but it's not required by the design contract specified by the List interface.There are other implementations of List in Java. Another very popular one is
LinkedList.LinkedListprovides the basic requirements ofListbut does so in a way that is better suited for cases where the length of the list that you want to work with can change a lot.So far, what has been described is an
interfaceordesign by contract. It's nice and helps to keep code structured and organized, but where interfaces really come into play is withpolymorphism.Ploymorphism lets you treat different classes that implement the same interface the same way. For example, let's say that we want to print out the contents of a list, any type of list. We could write a
print()method for every type of list possible but there's a smarter way to go about it. Rather than write one method for ArrayList and another for LinkedList, we can simply write one single method for List. Since ArrayList and LinkedList both implement the List interface, and the List interface requires that its classes provide aniteratormethod, we can simply call that iterator method on classes as if they were all List objects, instead of ArrayList and LinkedList objects.This means that we can write less code. And when we can write less code, there's less things that can go wrong. We're also able to create our programs quicker and faster.
Hopefully this helps you to understand why interfaces are an important part of the Java (and all other object-oriented) language.