Monday, December 31, 2018

Difference between Arraylist and HashMap?
  • ArrayList implements List Interface while HashMap is an implementation of Map interface.
  • Memory consumption is high in HashMap compared to the ArrayList.
  • ArrayList maintains the insertion order while HashMap doesn't maintain any order. ArrayList stores the elements only as value and maintain internally the indexing for every element. While HashMap stores elements with key and value pair, i.e. two objects. So HashMap takes more memory comparatively.
  • ArrayList allows duplicate elements but HashMap doesn't allow duplicate keys (It does allow duplicate values)
  • In ArrayList, an element can be fetched easily by specifying the index of it. But in HashMap, the elements is fetched by its corresponding key. It means that the key must be remembered always.
  •  In ArrayList, any number of null elements can be stored. While in HashMap, only one null key is allowed, but the values can be of any number.
ARRAYLISTHASHMAP
The java ArrayList implements List InterfaceThe java HashMap is implements Map interface
ArrayList always maintain insertion order of the elementsHashMap does not maintain the insertion order
ArrayList only stores value or elementHashMap stores key and value pairs
ArrayList can contain duplicate elementsHashMap does not contain duplicate keys but contain duplicate values.
We can have any numbers of null elements in ArrayListWe can have only one null key and any number of null values in HashMap
ArrayList get() method always gives an O(1) performanceHashMap get()method can be O(1) in the best case and O(n) in the worst case

Related Posts:

  • Factorial of a given number Factorial of a given number using recursion. public lass FactorialRecursion{         public static void main(String[] ar){              System.out.print("Enter a number … Read More
  • Extract digits from a String /* * Input: A14BC654 -> acceptable * Input: AB54C54D -> not acceptable  * Input: A23BED75 -> acceptable * Conditions to check:  * 1) First and Last index values of the string are character and digit respec… Read More
  • Difference between Iterator and Iterable? Both Iterator and Iterable are interfaces in Java Collection Framework. Here are some differences: Iterator: Iterator  is an interface that manages iteration over an Iterable. It maintains a state of where we are… Read More
  • Prime number a. prime or not Prime number: A prime number is a whole number greater than 1 whose only factors are 1 and itself. Ans: --- public class PrimeTest { public static void main(String[] args) { int n, i, k=0… Read More
  • Difference between Comparator and Comparable? Both are interfaces in Java. Here some common differences between Comparator and Comparable. Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package. Comparato… Read More

0 comments:

Post a Comment

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts