This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 3 years ago.
How do I decide which data structure to use for a more efficient program? I mostly use ArrayList.
I checked out other questions but I was not fully satisfied. If someone could help me I would be really happy.
As ArrayList and LinkedList both implement List interface. They are very similar to use. Their main difference is their implementation which causes different performance for different operations. ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array. LinkedList is implemented as a double linked list. Its performance on add and remove is better than ArrayList, but worse on get and set methods. So basically when you working with data that needs to be frequently added or removed from the list you would like to go for the LinkedList.
Related
This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 5 years ago.
I was a bit confused, when we use
List<String> lst = new LinkedList<>();
when we use
LinkedList<String> lklst = new LinkedList<>();
At the beginning, I thought they are the same, but today, I realized they are not the same. For example, if I call lst.getFirst() It will tell me there is a error. However, if i do lklst.getFirst(), it works fine. My question is when do we use lklst then? why they are different? Also, does it apply same rule for Map. THanks!
On the left hand side you're declaring the type of the variable, lst. Since lst's type is List you can only access methods of a List, even if the object lst points to is really a LinkedList. There's an inherent tradeoff between declaring a variable of a concrete type like LinkedList (access to more methods / behavior) vs. a more abstract interface (safer, better compartmentalized code).
This is a big topic, and there isn't one simple answer for when to do one vs. the other (though there's lots of advice out there about it!) - you'll need to figure out which is appropriate for your use case.
Effective Java - Item 52: Refer to objects by their interfaces is a pretty canonical citation for this issue, and as the title implies suggest preferring List rather than LinkedList.
This question already has answers here:
"Program to an interface". What does it mean? [duplicate]
(8 answers)
Closed 5 years ago.
I was reading this PowerPoint presentation when I came across this:
When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces. The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them.
Can anyone explain me about this concept? I referred few websites and book and I am still not clear about how this concept works. From above line it is known that it calls correct version of method. How does it work and when should I cast an object to interface type.
Let's give an example using the List interface. Two of its implementations are ArrayList and LinkedList. The first one is really fast at retrieving a random element (for example the 6th using get(5) method) but is slow at adding and removing elements. The second one is the opposite. Fast at adding and removing but slow at accessing random elements.
Now lets assume that you have a class that has methods that retrieve information about a car dealership. One method retrieves a list of all available car manufacturers while the other method retrieves all the cars the dealership has. In the first case you would want to use an ArrayList because you don't expect the list of manufacturers to change that much, whereas in the second case you want a LinkedList because you expect to sell and buy lots of cars thus making many changes.
However, the one who uses those methods, doesn't really care whether he handles an ArrayList or a LinkedList. All he wants to do is use get(x) and add(Car) or remove(Car) which are all methods of the List interface. Thus, your methods should all have List as their return type and they will decide what implementation they will provide, since it doesn't matter to the one calling them.
This also gives you the advantage of being able to, in the future, change the second method for example from providing a LinkedList into an ArrayList if you decide that you need fast retrieval instead of fast adding and removing. If the method was explicitly returning LinkedList you would have to go and change all the places that it was being called into the new type. But if it was returning simply the interface then no outside change is required!
This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 10 years ago.
I use this two statements to ArrayList definition:
ArrayList<String> my = new ArrayList<>();
List<String> my2 = new ArrayList<>();
Which one should i use?
With your second definition, you could later replace ArrayList constructor for another type of List, like a LinkedList or a high-performant list, or any other kind of list that may exist in the future. In your first definition, you are tied to the ArrayList implementation forever. You cannot change it, because the rest of your code is trusting in this reference being explicitly an ArrayList.
By using an interface, on the contrary, your code relies on a contract, that of the interface, and not in a particular implementation (like ArrayList). That gives you the power of changing implementations without affecting its users. And change is something that we must foster and plan ahead, simply because we cannot prevent things from changing.
The latter. Program to an interface, not to an implementation.
Depends on what you need. It's generally advised to use interfaces (hence List) if possible, otherwise you're sticked to implementation class. Say, you use ArrayList as input parameter of some method. For some reason (e.g. performance) you decide at some point to switch from ArrayList to LinkedList. So you must change also the type of the input parameter. If you use interface (List), you're more free to switch implementation without the need of refactoring code.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Array or List in Java. Which is faster?
I started a project where I will be constantly accessing an Arraylist that will never be changed. Would it be faster to access an ArrayList or an Array and by how much?
The difference will probably not be noticeable as the JVM will most likely optimise the call if you use it very often. And using List will give you a more robust and flexible code, so I would personally use Lists.
There is only one way to know: test it (after having read this post about how to test the performance of a Java application).
if its constant, how about an Enum, you can add logic directly to the enum instances.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why should the interface for a Java class be prefered?
ArrayList<Integer> al = new ArrayList<Integer>();
List<Integer> l = new ArrayList<Integer>();
what is the difference between these 2 lines? Is there any rules that I should use former one rather than later one in any case? Or vise versa? What is advantage or disadvantage of using particular one?
Thanks.
The first line creates an ArrayList and stores it in a variable of type of ArrayList, the second line stores it in a variable of type List.
List is an interface of which ArrayList is an implementation. The rule of thumb for deciding which type to store the instance in (List or a specific implementation, like ArrayList) is that you should store at the most generalized level suitable for your needs. This means that if you know that the variable must conform to behavior only exhibited by ArrayList and not a List in general, then you should use ArrayList, otherwise, use List. (This holds for LinkedList or other List implementations, too)
The reason why you want to use the second line it's because it's usually better to program to interfaces than to classes, sometimes it's related to good practices to do that and one of the benefits it's that you end up with code that:
It's better to test
Can use different implementations
It's not coupled to ArrayList
For more information you can take a look at the "Hollywood principle" or the Strategy pattern