List vs LinkedList vs ArrayList [duplicate] - java

This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 1 year ago.
On which basis should we choose to use List, LinkedList, ArrayList in Java?.
Also please explain which one is better to use?.

List is an interface, it should be the type of your variables or the arguments of your methods.
The other two are implementations of this interface.
LinkedList is optimized for inserts and deletes.
ArrayList is optimized for random access.
So, you can choose based on the kind of work you are going to do.

Related

Decide the type of collection for Java small desktop application [duplicate]

This question already has answers here:
What is the difference between a HashMap and a TreeMap? [duplicate]
(8 answers)
Hashset vs Treeset
(14 answers)
What does it mean to "program to an interface"?
(33 answers)
Closed 4 years ago.
I need to understand something for a homework at OOP course.
The objective is to simulate a small bank application.And I am confused at this part : "Implement the class Bank using a predefined collection which uses a hashtable. The hashtable key will be generated based on the account main holder."
So HashTable, HashSet and HashMap having are good to use.
Yet, can I also use TreeMap or TreeSet? I founded that java.util.TreeMap works in the same way. They even have same method put , remove etc.
To sum up , should I use only classes having "Hash" or can I use also the others like TreeMap?

Can we use LinkedList even though there is risk of memory overhead. In that isnt using ArrayList better option [duplicate]

This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 7 years ago.
I know that when it comes to search operation ArrayList is better. And when it comes to insertion and deletion operation LinkedList is better. But I have read that linked list will cause memory overhead. In that case is it still safe to use LinkedList. Is so in what situation we have to avoid using LinkedList even though our logic contains more of insertion and deletion operation
ArrayList is fast in search (iterating over elements), but LinkedList is fast in modifying (deleting, inserting in any position). Now it depends on you that what are you doing.
You can also refer to this stackoverflow answer

What is the difference between multiple implementations of ArrayList in the (Java8) source code [duplicate]

This question already has answers here:
Arrays.asList() doubt?
(8 answers)
Closed 9 years ago.
I was trying to understand Streams in Java8 and intermittently I stumbled upon an interesting thing in the source code of Java8: ArrayList seems to be implemented twice:
The obvious one: java.util.ArrayList
The non-obvious one: java.util.Arrays.ArrayList, which is a private class.
One odd difference is that the normal version is way bigger, and implements List<E>, whereas Arrays.ArrayList does not do so (directly).
Why is it defined twice? And why with the same name?
Actually its there ever since Arrays.asList() introduced. Array's ArrayList is view of the underlying array. If the Array gets changed the ArrayList will get effected and viceversa.
The main benefit, No additional space required because it wont copy the array to a new object (ArrayList), also no additional time to copy the elements.

Sorting an ArrayList by the value of a field in the objects it stores [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting ArrayList of Objects by Object attribute
Basically I have an ArrayList that stores objects, each of those objects has a field that stores an integer. I want to store the objects in my ArrayList by acending order of that integer.
Is there an easy way to do this, I've looked around the Java API doc but can't find anything that looks suitable, sorry if this is trivial but it sounds like it should be simple enough if I can just find the right documentation.
Thanks
Create your custom comparator class implementing java.util.Comparator and use java.util.Collections.sort(list, comparator).

How to make an array in Java read only? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Java unmodifiable array
Immutable array in Java
How do I make an array read only so that the elements inside it can only be read but cannot be modified,added or deleted. need to do this in JAVA. Please help. I think merely the use of final keyword wont help.Need to do something more than that at the code level. Thanks in advance!
Short answer is you can't -- final will only guarantee you that the reference to the array itself won't be changed. You can do this with a List though, as the Collections class provides a method for creating a List that cannot be modified (Collections.unmodifiableList) -- that is only if you can change your application to use List rather than array.

Categories

Resources