In Java when is it appropriate to use Array, ArrayList, and HashMap? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 26

In Java when is it appropriate to use Array, ArrayList, and HashMap?

different types of arrays and uses

9th Feb 2017, 8:39 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
8 Answers
+ 20
If you need an unchanging (after initialization) group of items/objects use an array. Arrays are immutable, altering them after initialization actually creates a new array. This can leave unreferenced arrays left over in memory waiting for garbage collection(GC). Not a big deal if you are making minor changes and remember to null an unused array to free it up for GC. However, if you are making a lot of changes repetitively, this can lead to leaving large amounts of unused arrays in memory waiting for GC. So, if you need to make changes to your List in this fashion or increase/decrease the size of it, you should use a List or ArrayList. If you need something that has key value pairs then you'd want to use a dictionary, map, hashmap.
9th Feb 2017, 9:56 PM
ChaoticDawg
ChaoticDawg - avatar
+ 8
A rule of thumb is to always use ArrayList unless you know you have a need for something. In reality it will make up about 80%+ of your collections. Arrays are for simpler static structures where the length can be known at initialisation time. HashMaps are dictionaries for when your index isn't an integer, the index is derived instead by calling the hash method of the objects you're using for keys. It's much more complicated but that's enough typing from my phone.
11th Feb 2017, 7:55 PM
Leon
Leon - avatar
+ 6
if you want performance and memory management is not your concern then arrays are best choice. if you want performance is not your concern in your design and memory management is your choice with less number of deletions then go for arraylist. if you want to represent a set of elements as individual value and key pair and later want to retrieve using key then hashmap is your friend.
11th Feb 2017, 9:24 AM
Abhishek C Prajapati
Abhishek C Prajapati - avatar
+ 3
When the size of your array use Array because it is fast as compare to arraylist when you work with Realtime project its matters alot. use Arraylist when you dont no about the size of Array... E:g Suppose you want to store 'n' number of student name in database then you have to use Arraylist because you dont no the size of n. Hashmap is different from these two it consists of pair of <key, value> where key is unique e:g suppose you want to store employee name with employee id then you can use hashmap.. But better to use hashtable it is synchronized and here null value is not allowed. So hashtable is good for realtime project..
14th Feb 2017, 7:24 AM
Muhammad Raza
Muhammad Raza - avatar
+ 2
Hasmap as the name suggests is like map.hasmaps are stored like (keys,values) or the other way round.Using keys you can store corresponding value and access them whenever you want.It is good for solving problems like adressbook. arraylist is used when you want to store 1d values but you are not certain of the length. Arrays are easy to use and you can use it whenever you want a number of elements of the same type and you have a fair idea of the number of elements.
19th Feb 2017, 2:15 PM
tanay
+ 1
anyone tell me the best site to download notepad++, have got notepad and the notepad++ I downloaded will not run any code, just getting error
14th Feb 2017, 3:12 PM
Bobby Martin
+ 1
arrays deal with a group of items (elements) e.g. if you wish to make a program that prints student ids and there names, you definately need to use arrays.
17th Feb 2017, 8:05 PM
Mark
+ 1
Array is simple and easy to use and element can't be changed at runtime since its element are initialized at first as per the size of array declared. If any sorting or other operation have to be done then we have to do it manually writing logic. ArrayList support runtime element insertion and searching of element is also fast with this. Many overloading method which are available in this class make easy to perform sorting,and other operation.Sort element basis of insertion order. HashMap deals with array having keys and value and, keys and value are of any data type. Hashmap sort element basis of key value.
20th Feb 2017, 8:45 PM
zrowhsi
zrowhsi - avatar