KEMBAR78
Java Collection Interview Answers 4yr | PDF | Computer Science | Computing
0% found this document useful (0 votes)
18 views5 pages

Java Collection Interview Answers 4yr

Uploaded by

bhumi031811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Java Collection Interview Answers 4yr

Uploaded by

bhumi031811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is the difference between ArrayList and LinkedList in terms of performance?

As a developer with 4 years of experience, Id say ArrayList provides faster access using index since

its backed by an array, while LinkedList offers better performance in insertion/deletion at arbitrary

positions, as it doesnt require element shifting.

2. Why is HashMap not thread-safe? How can you make it thread-safe?

HashMap isnt thread-safe because it doesnt synchronize access for concurrent threads. To make it

thread-safe, you can use Collections.synchronizedMap() or ConcurrentHashMap.

3. How does HashSet ensure uniqueness of elements?

HashSet uses a HashMap internally. It relies on the equals() and hashCode() methods to check for

duplicates.

4. What is the initial capacity and load factor in a HashMap?

Default initial capacity is 16 and default load factor is 0.75, which means it resizes when 75% of the

capacity is filled.

5. How does TreeMap maintain the order of keys?

TreeMap maintains natural ordering of keys or uses a custom Comparator provided at map creation.

6. What happens when you insert a duplicate key in a HashMap?

The new value overwrites the existing value associated with that key.

7. What is the use of Comparable?

Comparable is used to define the natural ordering of objects via the compareTo() method.

8. How does Collections.synchronizedList() work?


It returns a thread-safe list by synchronizing each method call of the list using synchronized blocks.

9. Can a HashMap have null keys and values?

Yes. HashMap allows one null key and multiple null values.

10. How does LinkedHashMap preserve insertion order?

LinkedHashMap maintains a doubly-linked list running through all its entries to preserve insertion

order.

11. What is the difference between HashMap and Hashtable?

HashMap is non-synchronized and allows null keys/values, whereas Hashtable is synchronized and

doesn't allow null keys or values.

12. What is the time complexity of get() and put() in HashMap?

In average cases, both operations are O(1), but in worst cases (e.g., hash collisions), they can be

O(n).

13. When would you prefer TreeSet over HashSet?

When you need elements to be sorted automatically, TreeSet is preferred.

14. What is the difference between fail-fast and fail-safe iterators?

Fail-fast throws ConcurrentModificationException on concurrent modification, while fail-safe doesn't,

as it works on a clone or uses internal locking.

15. Can a Set contain duplicate objects if their equals() returns false but hashCode() returns

the same?

No, Set relies on both equals() and hashCode(). If equals() returns false, it treats the object as
unique.

16. How can you remove duplicates from an ArrayList?

By converting it to a LinkedHashSet and back to a list, or using streams with distinct().

17. What is the output of iterating over a ConcurrentHashMap while modifying it?

It doesnt throw ConcurrentModificationException and reflects the updates done concurrently.

18. How do you avoid ConcurrentModificationException while iterating?

Use iterators provided by Concurrent collections or explicitly use iterator.remove().

19. Can you store heterogeneous objects in a List? How would that impact type safety?

Yes, using List<Object>. But it compromises type safety and requires casting.

26. How is collision handled in HashMap?

By chaining using linked lists or, since Java 8, balanced trees when collisions become frequent.

27. Explain treeification?

In Java 8+, when a bucket exceeds a threshold (typically 8), the linked list is converted into a

balanced tree for performance.

28. What is the significance of the threshold in HashMap?

Threshold = capacity * load factor; when size exceeds it, HashMap resizes.

29. Explain the internal working of ConcurrentHashMap in Java 8.

It uses a segmented lock strategy before Java 8. In Java 8, it uses synchronized + CAS for finer

control and better concurrency.


30. Can two unequal objects return the same hashCode()? What are the implications?

Yes. This causes collisions, and those objects will be placed in the same bucket, potentially

degrading performance.

31. How does TreeSet or TreeMap determine the order of elements?

By using compareTo() method of Comparable or a Comparator provided during construction.

32. Why should you override both equals() and hashCode() in key objects used in a

HashMap?

To ensure correct behavior. Equal objects must have equal hash codes to locate the correct bucket

and perform comparisons.

33. What happens internally when a HashMap resizes?

A new array is created with double the size, and all entries are rehashed and moved to the new

buckets.

34. What are some memory pitfalls when using large HashMaps?

Excessive capacity or frequent resizing can lead to memory overhead and GC pressure.

35. Explain how HashMap handles high collision scenarios. What changes were introduced

in Java 8 to improve performance?

Java 8 replaces long linked lists with balanced trees once the number of elements in a bucket

crosses 8, improving O(n) to O(log n).

36. What problems can arise if hashCode() and equals() are not implemented properly in a

key object used in HashMap?

Improper implementation can cause unexpected behavior like duplicate keys or inability to retrieve
values.

37. How does hash map works internally?

It computes a hash for the key, maps it to an index in the array, and handles collisions via chaining

or treeification.

38. Write code to iterate hashmap using iterator.

Map<String, String> map = new HashMap<>();

map.put("a", "apple");

Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();

while(it.hasNext()) {

Map.Entry<String, String> entry = it.next();

System.out.println(entry.getKey() + " : " + entry.getValue());

You might also like