Saravanan's Corner: Blackberry Dev

Saturday, 13 February 2021

What is hashCode?

The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure. We know that hash code is a unique id number allocated to an object by JVM. But actually speaking, Hash code is not an unique number for an object. If two objects are equals then these two objects should return the same hash code. So we have to implement hashcode() method of a class in such way that if two objects are equals, ie compared by equal() method of that class, then those two objects must return same hash code. If you are overriding hashCode you need to override equals method also.

What is the difference between Hashtable and HashMap?

Answer:
The basic differences are Hashtable is synchronized and HashMap is not synchronized. Hashtable does not allow null values, and HashMap allows null values.

What is the difference between ArrayList and LinkedList?

Answer:

Both ArrayList and LinkedList are implementation of List interface in Java. Both classes are non-synchronized. But there are certain differences as well.

Following are the important differences between ArrayList and LinkedList method.

Sr. No.
Key
ArrayList
LinkedList
1
Internal Implementation
ArrayList internally uses a dynamic array to store its elements.
LinkedList uses Doubly Linked List to store its elements.
2
Manipulation
ArrayList is slow as array manipulation is slower.
LinkedList is faster being node based as not much bit shifting required.
3
Implementation
ArrayList implements only List.
LinkedList implements List as well as Queue. It can acts as a queue as well.
4
Access
ArrayList is faster in storing and accessing data.
LinkedList is faster in manipulation of data.


No comments:

Post a Comment