Highlighted by Anderson Armani

See more

Enter LinkedList, it does not need to move all the elements of the list to insert or remove an element in between. Adding an element is always constant time as it actually has dynamic capacity. Other operations such as indexOf have same cost in both list implementations. And sort costs the same for all Collections because the sort method first dumps everything into an array. But LinkedList comes with an added constrain of not being able to access elements randomly. You can do get(index) but it would be a costly operation. This is the only place where ArrayList is better than a LinkedList. And I believe ArrayList should only be preferred over a LinkedList if random access is a necessity. More often than not I find that my code is using this feature due to my own laziness. Not because it was the best approach. To counter this ArrayList addiction start with initializing all your lists as LinkedList and only switch to an ArrayList if you can’t live without random access. If you are going to fetch a list of posts from a server and then iterate over them, using a LinkedList would make your code more elegant. Since instead of list.get(i) you would write list.next() list.previous() which is much more easier to understand that seeing a list.get(i) and then trying to figure out how i is changing. Using an ArrayList is justified for a music app where the user might hit the random button and so i has to be randomly changed.