Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: ArrayList for List, HashMap for Map, and HashSet for Set?

Tags:

java

I usually always find it sufficient to use the concrete classes for the interfaces listed in the title. Usually when I use other types (such as LinkedList or TreeSet), the reason is for functionality and not performance - for example, a LinkedList for a queue.

I do sometimes construct ArrayList with an initial capcacity more than the default of 10 and a HashMap with more than the default buckets of 16, but I usually (especially for business CRUD) never see myself thinking "hmmm...should I use a LinkedList instead ArrayList if I am just going to insert and iterate through the whole List?"

I am just wondering what everyone else here uses (and why) and what type of applications they develop.

like image 222
GreenieMeanie Avatar asked May 22 '09 02:05

GreenieMeanie


People also ask

Which is better ArrayList or Set?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.

Should I use HashSet or ArrayList?

If you have duplicate data in your code then you should use ArrayList otherwise you can use hashset as shown below So, if your code don't need the duplicate values then use Set instead of list because the set will give much better performance (O(n) vs O(n^2) for the list), and that's normal because avoiding duplicates ...

Can we use ArrayList in HashMap?

Array List can be converted into HashMap, but the HashMap does not maintain the order of ArrayList. To maintain the order, we can use LinkedHashMap which is the implementation of HashMap.

Which is faster ArrayList or HashSet?

As a conclusion, we can learn, that the contains() method works faster in HashSet compared to an ArrayList.


2 Answers

Those are definitely my default, although often a LinkedList would in fact be the better choice for lists, as the vast majority of lists seem to just iterate in order, or get converted to an array via Arrays.asList anyway.

But in terms of keeping consistent maintainable code, it makes sense to standardize on those and use alternatives for a reason, that way when someone reads the code and sees an alternative, they immediately start thinking that the code is doing something special.

I always type the parameters and variables as Collection, Map and List unless I have a special reason to refer to the sub type, that way switching is one line of code when you need it.

I could see explicitly requiring an ArrayList sometimes if you need the random access, but in practice that really doesn't happen.

like image 168
Yishai Avatar answered Oct 11 '22 07:10

Yishai


For some kind of lists (e.g. listeners) it makes sense to use a CopyOnWriteArrayList instead of a normal ArrayList. For almost everything else the basic implementations you mentioned are sufficient.

like image 24
Bombe Avatar answered Oct 11 '22 08:10

Bombe