Difference between HashMap & Hashtable in Java?

Difference between HashMap & Hashtable in Java?

Difference between HashMap & Hashtable in Java?

Difference between HashMap & Hashtable in Java?
  • The underlying data-structure is HashTable.
  • Insertion order is not preserved and it is based on HashCode is keys.
  • Duplicate keys are not allowed but values can be duplicated.
  • Heterogeneous objects are allowed for both Key and value.
  • Null is allowed for Key(only once).
  • Null is allowed for values(Any numbers of times).
  • HashMap implements the serializable and cloneable interface but not RandomAccess.
  • HashMap is the best choice if our frequent operation is a search operation.

Java Set is a collection of elements (Or objects) that contains no duplicate elements. Java Set is an interface that extends Collection interface. Unlike List, Java Set is NOT an ordered collection, it’s elements does NOT have a particular order. Java Set does NOT provide a control over the position where you can insert an element. You cannot access elements by their index and also search elements in the list.

Let’s look at the characteristics of each implementation in details:

  • HashSet: is the best-performing implementation and is a widely-used Set implementation. It represents the core characteristics of sets: no duplication and unordered.
  • LinkedHashSet: This implementation orders its elements based on insertion order. So consider using a LinkedHashSet when you want to store unique elements in order.
  • TreeSet: This implementation orders its elements based on their values, either by their natural ordering or by a Comparator provided at creation time.

Constructors 

1 HashMap m=new HashMap() Creates an empty HashMap object with default initial Capacity 16 and default fill ratio 0.75.
2 HashMap m=new HashMap(int initial capacity) Creates an empty HashMap object with specified initial capacity and default fill ratio 0.75
3 HashMap m=new HashMap(int initial capacity, float fill ratio) Creates an empty HashMap object with specified initial capacity and specified fill ratio.
4 HashMap m=new HashMap(Map m) create a HashMap with equivalent given Map object.

Leave a Reply

Your email address will not be published. Required fields are marked *

*