Thursday, 25 October 2012

Comparable And Comparator

Comparator and Comparable

In java these are the most common question what is comparable? What is comparator? Difference between comparable and comparator? When to use comparable? When to use comparator?
So in this blog I will explain you what and when to use comparable and comparator. 

Before going to when to use comparable and comparator answer this let’s have a look on common and different behaviour of both.
  •  Both are interfaces. 
  •  Both used for sorting purposes. 


Defined in java.util package
Defined in java.lang package
Has to override this method
public int compareTo(Object o)  
Has to override this method
public int compare (Object o1, Object o2) 
Used to implement natural ordering of object
Used to implement external ordering of object
Any class implements Comparable interface then collection of that class object (either List or Array) can be sorted automatically using Collections.sort() or Array.sort() method which sorts the objects in natural order or in ascending order.
To sort using Comparator, external comparator is passed as a parameter in Collections.sort
Comparable Class
Comparator Class

When to use Comparable 
  • If there is a natural or default way of sorting Object already exist during development of Class than use Comparable.

When to use Comparator 

  • If an Object can be sorted on multiple ways and client is specifying on which parameter sorting should take place than use Comparator interface.
  • Some time you write code to sort object of a class for which you are not the original author, or you don't have access to code. In these cases you cannot implement Comparable and Comparator is only way to sort those objects
  • Beware with the fact that how those object will behave if stored in SorteSet or SortedMap like TreeSet and TreeMap. If an object doesn't implement Comparable than while putting them into SortedMap, always provided corresponding Comparator which can provide sorting logic.

Wednesday, 24 October 2012

Eclipse Shortcut Keys

Eclipse is multi-language software development environment comprising an integrated development environment (IDE) and an extensible plug-in system.

Today's there are so many IDE's used for development purposes but eclipse captured 2/3 of the market.  

Eclipse Shortcut Keys
I wanted to share some shortcut keys to make developer more productive.


Ctrl + Shift + R
Open / Search for resources, e.g. files
Ctrl + Shift + T
Open / Search for Types
Ctrl + E
Allows to select an editor from the currently open editors
Ctrl + F8
Shortcut for switching perspectives
Alt + ← or Alt + →
Go to previous/ next editor position in history
Switch to previous/next editor
Go to the declaration of this variable
Ctrl + Shift + P
Go to the matching bracket


Ctrl + .
Go to the next problem / error
Ctrl + ,
Go to the previous problem / error
F4 on a variable
Show type hierarchy
Ctrl + J , Ctrl + K
Incremental search, find next
Ctrl + Shift + G
Search for references in the workspace


Ctrl + F11
Run last launched
Alt + Shift + X - J
Run current selected class as Java application

Editor Handling

Ctrl + 1
Quickfix; result depending on cursor position
Ctrl + Space
Content assist/ code completion
Ctrl + T
Show the inheritance tree of the current Java class
Ctrl + O
Show all methods of the current class, press Ctrl + O again to show the inherited methods.
Focuses on the editor (especially helpful if you work with Fast Views).
Ctrl + M
Maximize Java editor
Ctrl + Shift + F
Format source code
Ctrl + Shift + O
Organize the imports; will import the missing import statements.
Ctrl + Q
Go to position the cursor at the last changed position in the editor.
Ctrl + Alt + Z
Wrap the select block of code into a block, e.g. try/catch.

 Arrow Keys

Ctrl + Left
Move one element to the left
Ctrl + Right
Move one element to the right
Ctrl + Alt + Up/Down
Copy line
Alt + Up / Down
Move line up / down
Alt + Shift Up / Down
Select the previous / next syntactical element
Alt + Shift Up / Down / Left / Right
Extending / Reducing the selection of the previous / next syntactical element
Ctrl + Up / Down
Scroll up / down a line in the editor


Ctrl + D
Deletes line
Ctrl + Shift + DEL
Delete until end of line
Ctrl + DEL
Delete next element
Delete previous element

Variable Assignment

Ctrl + 2, L
Assign statement to new local variable
Ctrl + 2, F
Assign statement to new field


Shift + F2
Show the Javadoc for the selected type / class / method
Alt+Shift + N + Letter
Type shortcut for the command, e.g. njc to create a new Java class or npip to create a new Plugin project
Alt + Shift + Z
Surround block with try and catch


Alt + Shift + R
Ctrl+2, R
Rename locally (in file), faster then Alt + Shift + R
Alt + Shift + T
Opens the quick refactoring menu

Other Shortcuts
Ctrl + S
Saves current editor
Ctrl + 1
Quickfix; shows potential fixes for warnings, errors or shows possible actions
Ctrl + Space
Content assist/ code completion
Ctrl + Q
Goes to the last edited position
Ctrl+ D
Deletes current line in the editor
Ctrl + Shift + O
Adjusts the imports statements in the current Java source file
Ctrl + 2, L or F
Assign statement to new local variable or field
Ctrl + Shift + T
Open Type Dialog
Ctrl + O
Shows quick outline of a class
Ctrl + F11
Run last launched application
Ctrl + O
Shows quick outline of a class

Thanks for reading this. If you know any shortcut keys which is not in the above list please mentions in the comments.

Help4J - Jar Search Engine. Easiest way to find jar and its source.