Java Comparable Interface

Java allows comparing primitives values easily using the operators <, <=, >, >=, ==, !=.

But to compare objects using these operators are not supported and it is necessary to use a different approach.

How to compare two Tasks objects in a simple task manager system. Java allows comparing objects using Comparable interface. After defined how to compare the objects it is easy to sort the objects using a function available on Collections or Arrays classes.


A class is not comparable by default. To make a class comparable, the class needs to implement the interface Comparable and override the method compareTo().

The method compareTo() defines how the objects are compared between each other. Below is the definition of the Comparable interface.

public interface Comparable {
    public int compareTo(T o);

The method compareTo() returns an integer that defines if the object is less than, equal to or greater than the other one.

  • Negative – Indicates if the current object is less than the other object.
  • Zero – Indicates if the current object is equal to the other object.
  • Positive – Indicates if the current object is greater than the other object.

Java has many classes that implement Comparable interface in order to define the order of the object like String, Integer, Date, Calendar and many others.


The example shows how to implement the Comparable interface in a given class and how to define the method compareTo() to compare two different objects.

The example will compare the attribute priority using the methodcompareTo() defined in the Integer class.

import java.util.Date;
import java.util.Objects;
import java.util.UUID;

public class Task implements Comparable {

    private String id;
    private String name;
    private String description;
    private Integer priority;
    private Date created;
    private Status status;

    public Task() {

    public Task(String name, Integer priority) { = UUID.randomUUID().toString(); = name;
        this.priority = priority;
        this.created = new Date();
    // The Getters and Setters were omitted

    public int compareTo(Task anotherTask) {
        return this.priority.compareTo(anotherTask.priority);

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Task task = (Task) o;
        return id.equals(;

    public int hashCode() {
        return Objects.hash(id);

    public String toString() {
        return "{name=" + name + ", priority=" + priority +"}";


The compareTo() method could be implemented as shown below.

public int compareTo(Task anotherTask) {
   if(this.priority < anotherTask.getPriority()) {
       return -1;
   if(this.priority > anotherTask.getPriority()) {
       return 1;
   return 0;

The code below shows how to sort a list of given Tasks based on the order defined in the Task class.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ComparableTest {

    public static void main(String[] args) {

        List listOfTasks = new ArrayList<>();
        listOfTasks.add(new Task("Task 1", 1));
        listOfTasks.add(new Task("Task 2", 3));
        listOfTasks.add(new Task("Task 3", 2));
        listOfTasks.add(new Task("Task 4", 5));

        System.out.println("Before Sort");


        System.out.println("After Sort");
# output:

Before Sort
[{name=Task 1, priority=1}, {name=Task 2, priority=3}, {name=Task 3, priority=2}, {name=Task 4, priority=5}]

After Sort
[{name=Task 1, priority=1}, {name=Task 3, priority=2}, {name=Task 2, priority=3}, {name=Task 4, priority=5}]

WordPress with AWS Lightsail

Amazon Lightsail is a simple and cheap way to install a WordPress in just a few minutes. The installation takes less than 10 minutes and paying less than $5 USD per month. Lightsail is a “next-next-finish” or “click solution” to host simple applications like WordPress, Magento, Nginx or just a server with Linux or Windows.

The plan starts from $3.50 USD for a 512MB, 1 vCPU, 20GB. With full access over the instance and with SSH enabled. Lightsail provides the feature to create new instances from a snapshot, too cool to be real. See plans and prices.

Steps for the installation

1 – Select the instance location

2 – Select the instance platform between Linux or Windows

3 – Select the application

4 – Choose plan

5 – Create an instance name and tags

This process takes a few minutes. Now Amazon AWS is provisioning your brand new instance with the selected configuration. After this process, you can access your new WordPress blog.

The Instance details

6 – Connect in your instance using SSH

It is easy to connect into the instance using SSH terminal in your web browser. Click on the button “Connect using SSH” to open SSH terminal. The command below is to retrieve the WordPress password for the admin user “user”.

cat bitnami_application_password

The browser SSH terminal is handy but has limitations. It is easy to connect using SSH terminal on Mac/Linux or to use Putty for Windows. Download the certificate to SSH your instance. Click on the Account Page link

For the SSH connection is necessary to download the certificate. Without this certificate is not possible to connect in the instance.

Click on the download option to retrieve the LightsailDefaultKey.pem. Copy it to a folder of your choice. To avoid the error below is necessary to need to grant the correct permissions to the certificate file LightsailDefaultKey.pem

The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:LGI6TDbm4DL+gSpPQXVnU6L2r8zhx59C7+MCs/D0h1A.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '' (ECDSA) to the list of known hosts.
Permissions 0644 for 'LightsailDefaultKey-eu-west-1.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Permissions 0644 for 'LightsailDefaultKey-eu-west-1.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "LightsailDefaultKey-eu-west-1.pem": bad permissions

The command below is to grant the permissions.

sudo chmod 0644 LightsailDefaultPrivateKey-eu-central-1.pem

Connecting into the instance using SSH terminal. Run the command replacing the IP for your own instance IP address.

ssh bitnami@ip_address -i LightsailDefaultKey-eu-west-1.pem

Login into your new WordPress admin

First, you need to retrieve the password generated by Bitnami. Connect into your instance via SSH and run the command below. It prints out the default user password. Now you can log in to the WordPress using the credentials “user” and the password retrieved from the command below.

cat bitnami_application_password

Don’t forget to change the password after your first access. Go to your profile and change the password or create a new admin user and delete the old one it is to avoid malicious connections into your WordPress application.

Disabling Bitnami banner

The Bitnami banner is displayed at the bottom right corner of the page. This banner will show some information regarding your WordPress setup

SSH the instance and run the commands below

sudo /opt/bitnami/apps/wordpress/bnconfig --disable_banner 1
sudo /opt/bitnami/ restart

To point your new WordPress to a DNS server is better to configure a static IP. Select the “Networking” tab and then click on “Create static IP” button.

Connect into your DNS Server Provider like Hostgator and place the entries below.

@ IN A ip_address
www IN ip_address

Now you have your own WordPress instance and it took less than 10 minutes.