Sec 10: Java Collection Questions
Last updated
Last updated
List can contain duplicate elements whereas Set contains only unique elements.
HashSet maintains no order whereas TreeSet maintains ascending order.
Set contains values only whereas Map contains key and values both.
HashSet contains only values whereas HashMap contains entry(key,value). HashSet can be iterated but HashMap need to convert into Set to be iterated.
HashMap maintains no order but TreeMap maintains ascending order.
Collection is an interface whereas Collections is a class. Collection interface provides normal functionality of data structure to List, Set and Queue. But, Collections class is to sort and synchronize collection elements.
If you change the value in properties file, you don't need to recompile the java class. So, it makes the application easy to manage.
The hashCode() method returns a hash code value (an integer number). The hashCode() method returns the same integer number, if two keys (by calling equals() method) are same. But, it is possible that two hash code numbers can have different or same keys.
The equals method is used to check whether two objects are same or not. It needs to be overridden if we want to check the objects based on property.
For example, Employee is a class that has 3 data members: id, name and salary. But, we want to check the equality of employee object on the basis of salary. Then, we need to override the equals() method.
If we use generic class, we don't need typecasting. It is typesafe and checked at compile time.
Two different keys with the same hash value is known as hash-collision. Two different entries will be kept in a single hash bucket to avoid the collision.
The Dictionary class provides the capability to store key-value pairs.
The default size of load factor is 0.75. The default capacity is computed as initial capacity load factor. For example, 16 0.75 = 12. So, 12 is the default capacity of Map.