+ 26
In Java when is it appropriate to use Array, ArrayList, and HashMap?
different types of arrays and uses
8 Réponses
+ 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.
+ 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.
+ 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.
+ 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..
+ 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.
+ 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
+ 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.
+ 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.