Core Java - Interview Questions and Answers for 'Treemap' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Treemap' - 17 question(s) found - Order By Rating

 Q1. Difference between TreeMap and SortedMap ?Core Java
Ans. sortedMap is an interface ,while TreeMap is an implementation of sortMap.

We can not create an instance of sortedMap but we can create an instance of treeMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     treemap  sortedmap


 Q2. How to sort objects based on one of the field ?Core Java
Ans. Using comparable and comparator and sorted collections like TreeSet or TreeMap.

or

use stream api from java 8 onwards which internally refers to comparable and comparator through lambda expressions

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     sort  sorting  comprator  comparable  treeset  treemap  sorting collections     Asked in 1 Companies      Basic        frequent


 Q3. What is the difference between TreeSet and TreeMap ?Core Java
Ans. TreeSet contains only values as elements whereas TreeMap contains Key value pairs.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     reeMap  TreeSet  Collections  sorted collection     Asked in 1 Companies        rare


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q4. Difference between TreeMap and HashMap ?Core Java
Ans. They are different the way their elements are stored in memory. TreeMap stores the Keys in order whereas HashMap stores the key value pairs randomly.

  Sample Code for treemap

  Sample Code for hashmap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   map   treemap   hashmap   treemap vs hashmap     Asked in 31 Companies      basic        frequent

Try 1 Question(s) Test


 Q5. What is comparator interface used for ?Core Java
Ans. The purpose of comparator interface is to compare objects of the same class to identify the sorting order. Sorted Collection Classes ( TreeSet, TreeMap ) have been designed such to look for this method to identify the sorting order, that is why class need to implement Comparator interface to qualify its objects to be part of Sorted Collections.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   treeset   comparator     Asked in 2 Companies      Intermediate

Try 2 Question(s) Test


 Q6. Which are the sorted collections ?Core Java
Ans. TreeSet and TreeMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   treeset   basic interview question      basic        frequent


 Q7. How can we reverse the order in the TreeMap ?
Ans. Using Collections.reverseOrder()

Map tree = new TreeMap(Collections.reverseOrder());

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   map   treemap

Try 1 Question(s) Test


 Q8. TreeMap orders the elements on which field ?
Ans. Keys

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   map   treemap


 Q9. How TreeMap orders the elements if the Key is a String ?
Ans. As String implements Comparable, It refers to the String compareTo method to identify the order relationship among those elements.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   comparable interface   treemap   compareto


 Q10. Can we add heterogeneous elements into TreeMap ?
Ans. No, Sorted collections don't allow addition of heterogeneous elements as they are not comparable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   comparable interface   treemap


 Q11. Will it create any problem if We add elements with key as user defined object into the TreeMap ?
Ans. It won't create any problem if the objects are comparable i.e we have that class implementing Comparable interface.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   comparable interface


  Q12. Can we have null keys in TreeMap ?Core Java
Ans. No, results in exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap     Asked in 6 Companies      Basic        frequent


 Q13. Can value be null in TreeMap ?Core Java
Ans. Yes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   yes-no


 Q14. Which interface TreeMap implements ?
Ans. TreeMap implements NavigableMap, SortedMap, Serializable and Clonable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap


 Q15. Which tree is used to implement TreeMap in Java?
Ans. Red Black Tree

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   map      expert


 Q16. Which of the following collection maintain its elements in Natural Sorted order ?

a. HashMap
b. TreeMap
c. LinkedHashMap
d. LinkedMap
Ans. TreeMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     treemap   collections   java   sorted collections   map


 Q17. If we add Enum constants to a sorted collection ( Treemap , TreeSet ), What will be the order in which they will be maintained ?

a. Sorted Collection wont maintain them in any order.
b. Insertion Order
c. Order in which constants are declared.
d. Natural Sorting Order.
Ans. Order in which constants are declared.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum   collections   sorted collections   treemap   treeset   compareto   comparable   java



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: