What is the difference between "ArrayList" and "HashSet" in Java?

Posted by  ShriKant Mishra
 51  View(s)
Ratings:
Rate this:
  1. What is the difference between "ArrayList" and "HashSet" in Java?

    1) The first and most important difference between ArrayList and HashSet is that ArrayList implements List interface while HashSet implements Set interface in Java.

    2) Any other difference between ArrayList and HashSet is that ArrayListallow duplicates while HashSet doesn't allow duplicates. This is the side effect of the first difference and property of implementing the List and Set interface.

    3) The third difference between ArrayList and HashSet is that ArrayList is an ordered collection and maintains the insertion order of elements while HashSet is an unordered collection and doesn't maintain any order.

    4) The fourth difference between ArrayList and HashSet is that ArrayList is backed by an Array while HashSet is backed by a HashMap instance.

    5) The fifth difference between HashSet and ArrayList is that its index-based you can retrieve an object by calling get(index) or remove objects by calling remove(index) while HashSet is completely objective-based. HashSet also doesn't provide the get() method.

    That's all about the difference between ArrayList and HashSet.

      Modified On Mar-12-2020 02:02:36 AM

Answer