Home » Collection

Collection

Last Updated on July 9, 2023 by KnownSense

51 What is a weak HashMap?
An implementation of the map interface, which includes only weak references to the keys. This allows garbage collection when the key is no longer referenced outside of the Map.

map interface

52. Why concurrentHashMap is not used over HashMap?
This is because it doesn’t perform well in a multithreaded environment.

53. How to avoid ConcurrentModificationException while iterating a Collection?
We can use concurrent collection classes to avoid `ConcurrentModificationException` while iterating over a collection, for example CopyOnWriteArrayList instead of ArrayList.

54. What is UnsupportedOperationException?
UnsupportedOperationException is the exception used to indicate that the operation is not supported. It’s used extensively in classes, in collections framework `java.util.Collections.UnmodifiableCollection` throws this exception for all `add` and `remove` operations.

55. What is the importance of hashCode() and equals()  method?
HashMap uses the Key object hashCode() and equals() method to determine the index to put the key-value pair. These methods are also used when we try to get value from HashMap. If these methods are not implemented correctly, two different Key’s might produce the same hashCode() and equals() output and in that case, rather than storing it at a different location, HashMap will consider the same and overwrite them. Similarly all the collection classes that doesn’t store duplicate data use hashCode() and equals() to find duplicates, so it’s very important to implement them correctly. The implementation of equals() and hashCode() should follow these rules.
–   If `o1.equals(o2)`, then `o1.hashCode() == o2.hashCode()`should always be `true`.
–   If `o1.hashCode() == o2.hashCode` is true, it doesn’t mean that `o1.equals(o2)` will be `true`.

56. Which Collection classes are thread-safe?
Vector, Hashtable, Properties and Stack are synchronized classes, so they are thread-safe and can be used in multi-threaded environment. Java 1.5 Concurrent API included some collection classes that allows modification of collection while iteration because they work on the clone of the collection, so they are safe to use in multi-threaded environment.

57. How can we make synchronized collection from a given collection?
We can use `Collections.synchronizedCollection(Collection c)` to get a synchronized (thread-safe) collection backed by the specified collection.

58. What are the best practices for java collection framework?
Chosing the right type of collection based on the need, for example if size is fixed, we might want to use Array over ArrayList. If we have to iterate over the Map in order of insertion, we need to use LinkedHashMap. If we don’t want duplicates, we should use Set.
–   Some collection classes allows to specify the initial capacity, so if we have an estimate of number of elements we will store, we can use it to avoid rehashing or resizing.
–   Write program in terms of interfaces not implementations, it allows us to change the implementation easily at later point of time.
–   Always use Generics for type-safety and avoid ClassCastException at runtime.
–   Use immutable classes provided by JDK as key in Map to avoid implementation of hashCode() and equals() for our custom class.
–   Use Collections utility class as much as possible for algorithms or to get read-only, synchronized or empty collections rather than writing own implementation. It will enhance code-reuse with greater stability and low maintainability.

59. What is the advantage of Properties file?
If you change the value in the properties file, you don’t need to recompile the java class. So, it makes the application easy to manage. It is used to store information which is to be changed frequently

60. What is hash-collission?
Two different keys with the same hash value are known as hash-collision. Two separate entries will be kept in a single hash bucket to avoid the collision. There are two ways to avoid hash-collision.

  • Separate Chaining
  • Open Addressing

61. How to convert ArrayList to Arrays ana vice versa?
We can convert an Array to ArrayList by using the asList() method of Arrays class. asList() method is the static method of Arrays class and accepts the List object. Consider the following syntax:
Arrays.asList(item)  
We can convert an ArrayList to Array using toArray() method of the ArrayList class. Consider the following syntax to convert the ArrayList to the List object.
List_object.toArray(new String[List_object.size()])  


Authored by codingknownsense.com

Scroll to Top