Blog Archives

06: HashMap & HashSet and how do they internally work? What is a hashing function?

HashMap & HashSet are not only one of the frequently used data structures, but also one of the popular interview topics.

Q1. How does a HashMap store data?
A1. As key/value pairs. You can store and retrieve values with the keys.

Q2. What is the HashMap lookup time in Big O notation?
A2. On average it is O(1) if the hashCode() method spreads out the buckets as discussed below. A list has O(n) and a sorted list has O(log n) to find an element.

Q3 How does a HashMap internally store data?
A3. A backing array for the buckets and a linked list (until Java 8) and a “binary tree” (Java 8 onwards) to store the values..

Backing Array with buckets: as shown below.

1) When you put an object into a map with a key and a value, hashCode() method is implicitly invoked, and hash code value say 123 is returned. Two different keys can return the same hash value. A good hashing algorithm spreads out the numbers. In the above example, let’s assume that (“John”,01/01/1956) key and (“Peter”, 01/01/1995) key return the same hash value of 123.

2) Now when a hashCode value of say 123 is returned and the initial HashMap capacity is say 10, how does it know which index of the backing array to store it in? This is where the HashMap internally invokes the hash(int ) & indexFor(int h, int length) methods.… Read more ...



5 Java Object class methods interview questions & answers

Q1. What are the non-final methods in Java Object class, which are meant primarily for extension? A1. The non-final methods are equals( ), hashCode( ), toString( ), clone( ), and finalize( ). These methods are meant to be overridden. The equals( ) and hashCode( ) methods prove to be very…

Read more ...
Tags: , ,

8 Java immutable objects interview questions & answers

Best Practice: “Classes should be immutable unless there’s a very good reason to make them mutable….If a class cannot be made immutable, limit its mutability as much as possible.” — by Joshua Bloch Q1. What is an immutable object? Q2. Immutable objects are objects whose state (the object’s data) cannot…

Read more ...
Tags: , ,

Java enum interview questions & answers

Covers the power of enumand 3 design patterns that can be applied with the help of enums. Q1. Why is it a best practice to use enums over static final int or String constants? A1. 1) static final allows invalid values. Any integer value can be assigned, although there are…

Read more ...


Java serialization, cloning, and casting interview Q&A

Q1. Which Java interface must be implemented by a class whose instances are transported via a Web service? a. Accessible b. BeanInfo c. Remote d. Serializable A1. Answer is “d”. Q2. What is serialization? A2. Object serialization is a process of reading or writing an object. It is a process…

Read more ...
Tags: , ,

Mocks, stubs, domain, and anemic objects interview Q&A

Q1. What are mock objects? A1. Mock objects are used in unit testing to ensure that your tests don’t fail due to volatility of the data changes. There are mocking frameworks like EasyMock, Mockito, and PowerMock. The key point to remember regarding mock objects is the ability of the mock…

Read more ...
Tags: ,

Object equals Vs == and pass by reference Vs value

Q1. What is the difference between “==” and equals(..) method when comparing 2 objects?
A1.It is important to understand the difference between identity (i.e. ==) comparison, which is a shallow comparison that compares only the object references, and the equals( ) comparison, which is a deeper comparison that compares the object attributes. The diagram below explains the difference between the two. There are some exceptional conditions when using primitives, String objects, and Enums.

Q2. What happens if you don’t override the “equals(..)” method in a user defined class like Pet.java?
A2. The equals(..), hashCode(), toString(), clone(), and finalize() are non-final methods in the java.lang.Object, which are meant primarily for extension. Implicitly every user defined class in Java extends the java.lang.Object class. So,

implicitly means,

If the equals(..) method is not overridden in the user defined classes, then the Object class’s default implementation is invoked, which typically is implemented by converting the internal address of an object into an integer. This only compares the object references. Invoking the equals(..) method of the Object class is equivalent to making a shallow comparison with “==”. This is why it is imperative to override the equals(…) hashCode( ) methods in the user defined classes like “Pet.java”.… Read more ...

Tags: , ,

500+ Enterprise & Core Java programmer & architect Q&As

Java & Big Data Tutorials

Top