Core Java - Interview Questions and Answers for 'Comparable' | 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 'Comparable' - 8 question(s) found - Order By Rating

 Q1. 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


 Q2. Where do we generally use comparable interface ?Core Java
Ans. Implementing Comparable interface means that the elements of the class are comparable i.e the class provides the implementation of compareTo method that would help comparing the elements.

This is usually required if we are planning to sort elements of a collection, If compareTo method is not defined , the sorting class / method could never understand a way to compare its elements in order to sort them.

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

   Like         Discuss         Correct / Improve     comparable interface

Try 1 Question(s) Test


Frequently asked in CTS ( Based on 2 feedback )
  Q3. What is the difference between comparable and comparator in java.util pkg?Core Java
Ans. Comparable interface is used for single sequence sorting i.e.sorting the objects based on single data member where as comparator interface is used to sort the object based on multiple data members.

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

   Like         Discuss         Correct / Improve     java   java.util   comparable   comparator   collections     Asked in 23 Companies      basic        frequent


  Q4. What is Comparable Interface?Core Java
Ans. It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.

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

   Like         Discuss         Correct / Improve     java   collections   comparable interface     Asked in 7 Companies      intermediate        frequent

Try 1 Question(s) Test


 Q5. 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


 Q6. 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


 Q7. 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


 Q8. 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


 Q9. Which method needs to be implemented if a class is implementing comparable interface ?Core Java
a. comp
b. compare
c. compareTo
d. compareEquals

Ans.c. compareTo


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: