0% found this document useful (0 votes)
8 views3 pages

Java Q and A

The document provides a series of questions and answers related to Java Collections, covering key differences between various collection types such as List, Set, HashSet, TreeSet, and Map. It explains concepts like hash collisions, the importance of overriding equals() and hashcode() methods, and the advantages of using generic collections. Additionally, it discusses practical aspects like printing arrays and the default load factor in hashing-based collections.

Uploaded by

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

Java Q and A

The document provides a series of questions and answers related to Java Collections, covering key differences between various collection types such as List, Set, HashSet, TreeSet, and Map. It explains concepts like hash collisions, the importance of overriding equals() and hashcode() methods, and the advantages of using generic collections. Additionally, it discusses practical aspects like printing arrays and the default load factor in hashing-based collections.

Uploaded by

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

JAVA Collections Ques and Ans

1) What is the difference between list and set?

Ans: List can contain the duplicate elements.

Set contains only unique elements.

2) What is the difference between Hashset and Treeset?

Ans: Hashset maintains no order whereas Treeset maintains the ascending order.

3) What is the difference between set and map?

Ans: set contains only values whereas Map contains both key and values.

4) What is the difference between Hashset and HashMap?

Ans: Hashset contains only values whereas HashMap Contains entry (key,value).

Hashset can be iterated but HashMap need to convert into Set to be iterated.

5) What is the difference between Hashmap and Treemap?

Ans: Hashmap maintains no order whereas Treemap maintains the ascending order.

6) What is the difference between Collection and Collections?

Ans: 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.

7) What is the advantage of Properties file?

Ans; 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.

8) What does the hashcode() method will do?

Ans: The HashCode() method returns a hashcode 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.

9) Why we override equals()method?

Ans: The Equals method is used to check whether two objects are same or not. It need to be
overridden if we want to check the objects based on property.

For example, emp is a class has 3 data members id ,name and salary. But , we want to check the
equality of emp object on the basis of salary. Then we need to overrode the equals() method.
10) What is the advantage of the generic collection?

Ans: if we use generic class, we don’t need typecasting. It is typesafe and checked at compile
time.

11) What is the Hash collision in Hashtable and how it is handled in Java?

Ans : 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

12) What is the difference between Poll() and Remove() method?

Ans: Both the poll() and remove () take out the object from the queue but if poll( ) fails then it
returns null but if remove fails it throws exception.

13) What is the difference between linkedHashmap and PriorityQueue in Java?

Ans: PriorityQueue guarantees that lowest or highest priority element always remain at the head
of the queue, but the LinkedHashmap maintains the order on which elements are inserted.
When you iterate over PriorityQueue, Iterator doesn’t guarantee any order but iterator of
LinkedHashMap does guarantee the order on which elements are inserted.

14) What is a couple of ways that you could sort a collection?

Ans:You can either use the Sorted Collection like Treeset or TreeMap or you can sort using the
ordered collection like a list and using Collections.sort() Method.

15) Is it Possible for two unequal objects to have same hashcodes?

Ans : Yes, two unequal objects can have same hashcode that’s why collision happen in hashmap.
The equal hashcode contract only say that two equal objects must have the same hashcode it
doesn’t say anything about the unequal object.

16) Can we use random numbers in the hashcode() method?

Ans : No, because hashcode of an object should be always same .

17) Why you need to override hashcode , when you override equals in java?

Ans: Because equals have code contract mandates to override equals and hasdcode together.
Since many container class like Hashmap or Hashset depends on hashcode and equals contract.
18) Can two equal object have the different hashcode?

Ans: No, That not possible according to hash code contract.

19) What is the dictionary Class?

Ans: The Dictionary Class provides the capabitlity to store key value pairs.

20) Can I Write my own container class and use it in the for each loop?

Ans: Yes, you can write your own container class. You need to implement the Iterable interface if
you want to loop over advanced for lopp in Java though. If you implement Collection then you by
default get that property.

21) How do you print Array in Java?

Ans: You can print array by using the Arrays.toString() and Arrays.deepToString() method. Since
array doesn’t implement toString()by itself, Just passing an array to System.out.println() will not
print its connects but Arrays .to String() will print each element.

22) What is the default size of load factor in hashing based collection?

Ans: The default size of load factor is 0.75. The default capacity is computed as initial capacity *
load factor. For eg, 16 * 0.75 = 12, 12 is the default capacity of Map.

You might also like