r/javahelp • • Oct 02 '21

Can someone please explain the difference between List and ArrayList like I'm five?

What exactly is the difference between

List<String> inputs = new ArrayList<>()

and

ArrayList<String> inputs = new ArrayList<>()

I've read some stuff about it but I'm a bit confused and I don't feel like I understood completely. Can someone explain the difference with some examples? Thank you!

30 Upvotes

29 comments sorted by

View all comments

43

u/RhoOfFeh Oct 02 '21

If someone gives you a box, you can put things in it, right?

Does it matter if that's a cardboard box, or a wooden one, or a metal one? Well, maybe one or another is better for what you want to put in it, but you can always use a box the same way.

A List is like a box. An ArrayList is like, oh, let's say a wooden box. There are other kinds of Lists available, but most of the time the code just doesn't care what KIND of List, just so long as it IS a List of some sort.

6

u/cervantesrvd Oct 02 '21

That's the easiest and best explanation/comparison I've seen. Will tell that to my newbie friend. Kudos and thanks mate!

1

u/RhoOfFeh Oct 02 '21

Thank you, I appreciate it.

2

u/w1Ld_D0G Oct 03 '21

Amazing analogy, I'm gonna use it!

1

u/RhoOfFeh Oct 03 '21

Man, I can't wait until the checks start rolling in, lol

3

u/coloneleranmorad Oct 02 '21

okay then in which scenario I should use a list or arraylist? according to what you're saying, then I can always use a list, as long as I define that list as arraylist? I know it is a no but I'm trying to understand why.

15

u/ignotos Oct 02 '21

Ultimately you always need to pick a specific ("concrete") type of list - like an ArrayList or LinkedList.

But the variable you use to refer to that list can just be defined as a generic "List". Then you can just change the specific kind of list you assign to it (e.g. List<String> inputs = new ArrayList<>() vs List<String> inputs = new LinkedList<>()), and the rest of your code will continue to work as expected.

The only reason to define that variable as a concrete type - like ArrayList<String> inputs - is if the rest of your code specifically requires an ArrayList, and won't work at all with other types of List. This is quite rare in practice. Sometimes you need to do this if you are using specific "special features" which an ArrayList has which other types of list do not.

5

u/RhoOfFeh Oct 02 '21

The simple rule for me is this: Use the interface as everywhere as I can.

In other words, up until using List gives me a syntax error because there's no such thing as 'new List()', I'm not specifying a concrete type anywhere.

4

u/RushTfe Oct 02 '21

You can use a list in every single scenario you would have used arraylist.

The only downside of using list instead of arraylist is that you won't be able to use the specific arraylist functions.

Following the box example.

You have a box, and it will only let you put things in.

But if you have a lockableBox, you will not only be able to put things in but also close and lock it.

If you say your lockableBox is just a box, you won't be able to close it, but you could still be able to put things in.

So unless there's a very specific reason where you need your list to be a arraylist, you can just say it's a list and move on.

I've been coding in java for 4 years, and I still haven't seen a situation where using arraylist would be better option than just a list.

Other implementations of list is a different thing.

3

u/MrSquicky Oct 02 '21

When you are using it and all you care about is that it is a list, you should be using List.

A list is a descriptive category for things that contain elements. Because it is just a concept, it is not a thing itself, but rather describes how things that match that concept can be used. It describes what things that are Lists can do, but not how they do it. Because of this, when you are actually making something to contain things, your can't just make a List, but rather have to make an actual thing, like an ArrayList, that has the instructions to do what a List says it can do.

When you are using a List, you generally only care about what it is doing, not how those things are being done and you may, for various reasons unrelated to context on which you are using the list, want to use things that perform either the List actions in different ways or add different types of functionality along with list stuff. So, instead of having to write code for each of the actual things that do this on different ways, you just write one path that uses List.

So, you need ArrayList when you are creating an actual thing, but other than that, your code is relating to it as an abstract concept and should be dealing with a list. A large part of code is communication based on your choices, and If you are choosing to give more detail than List in your code, it will be taken as a sign that the additional information provided by that is important or, more likely, that you are a novice programmer.

2

u/morhp Professional Developer Oct 02 '21

Unless you need a specific list type for some reason (you probably don't) always use List<...> for the variable type.

If you want to create a new list new ArrayList<>() on the right ride is usually the right approach if you need a list that you can change later, but you can create lists in many other ways, like new LinkedList<>() or Arrays.asList() or List.of(...) or List.copyOf(...).

You can't write new List<>() directly, as Java wouldn't know what type of List to create (like if you want a wooden or metal box), if you use methods like List.copyOf() Java will decide that for you and automatically choose a good implementation for the amount of items and so on from inside the method.

2

u/Bodine12 Oct 02 '21 edited Oct 02 '21

You will never use a List, because it’s an interface. You must use an implementation of a list, like ArrayList or LinkedList. Each one has different ways of implementing the common interface methods. So if you decide to change from arraylist to linkedlist it’s no problem because the different implementation will be called. However, if you explicitly declare ArrayList = New ArrayList you can no longer do that because the variable is bound to the concrete type ArrayList. Edited to add: The cases where you would want to limit yourself to a concrete type would be when the concrete type has methods that aren’t on the List interface. LinkedList especially. If you would declare “List myList = new LinkedList” then you will only have List interface methods available to you. If instead you declare “LinkedList mylist = new LinkedList()” then you’ll have all the LinkedList methods, but then the trade off is that variable couldn’t be switched to an ArrayList because ArrayList doesn’t have those extra methods.

1

u/denisfalqueto Oct 03 '21

You should get familiar to "coding to interfaces", that's the general technique. An introductory article is this one; https://www.infoworld.com/article/2076468/smarter-java-development.html