Array | ArrayList |
- Stores primitive data types and objects, both
| - Stores only objects. This is not to say that we cannot work with primitive data types in ArrayList. Autoboxing feature of java helps us circumvent this.
- Eg., this works:
- ArrayList<Integer> integerList = new ArrayList<Integer>();
- Here we are not storing primitive in ArrayList, instead autoboxing will convert int primitive to Integer object
|
- Defined in Java as a fundamental data structure itself
| - Belongs to Collections framework
|
- Array is a fixed length data structure. [Static nature]
- Fixed size - the size of the array has to be defined while declaring the array
- In an array of size 10 you can set to null your element at index 5. Your array will still have 10 elements numbered 0 to 9 but with an empty element at index 5.
| - ArrayList is a variable length Collection class.
- Automatically shrinks/grows, as and when elements are removed/added. [This is its Dynamic nature]. We can create instance of ArrayList without specifying size
|
- Stores similar data of one type
| - Can store heterogeneous data types
|
- Cannot use Generics with Array, because Array instance must know about what kind of data type it can hold, and throws ArrayStoreException, if you try to store a type which is not convertible into type of Array
| - Can use Generics with Array
|
- No of elements = Array.length
| - No of elements = ArrayList.size()
|
- You can simply use assignment operator to store element into Array
- objArray[1] = new Object();
| - You have to use add() method to insert element into ArrayList.
|
| - It is a class with many methods
|
- Cannot be synchronized, so cannot be used in multithreaded env
| - Can be obtained a synchronized version
|
- Elements retrieved with For loop
| - Can be retrieved with For loop and iterators
|
- Elements accessible with index number
| - Accessing methods like get() etc. are available
|
| |
| |
| |
| |
ArrayList | HashMap |
- ArrayList implements List Interface
| - HashMap is an implementation of Map interface
|
- ArrayList maintains the insertion order
| - HashMap doesn�t maintain any order, the returned key-values pairs are not sorted in any kind of order
|
- ArrayList allows duplicate elements
| - HashMap doesn�t allow duplicate keys (It does allow duplicate values)
|
| - If you want an array of all the keys in the hash map, you can use the keySet() method
|
No comments:
Post a Comment