The Comparable Interface: [java.util.Comparable]

The Comparable Interface: [java.util.Comparable]

This interface defines the way in which objects are to be compared. Anobject that implements this can be sorted as easily as any primitive, such asthe [String] class…

 import java.util.*;import java.awt.*;class SortStringArguments			// Sorts the arguments in args.{	public static void main(String[] args)	{		Arrays.sort(args);		for (int i=0; i

If the objects do not implement Comparable? You can modify the objects toimplement Comparable.

To make an object comparable:
1) Add Comparable to the object's implements list.
2) Make the object implement the compareTo() method.

The compareTo() method compares the an object with another of the same type.If the object is to come before the other, compareTo() should return anegative number. If the object should appear after the other object,compareTo() should return a positive number. Zero should be returned if theobjects are equal in the comparison.

Point is an AWT class that is not comparable. This example sorts based ondistance from the origin (0, 0):

 import java.util.*;import java.awt.*;class MyPoint extends java.awt.Point implements Comparable{	MyPoint(int x, int y)	{		super(x, y);			// Superimpose on the original (x, y) kinda like a union	}	public int compareTo(Object o)	// compareTo() is supposed to take an objectparameter	{		MyPoint p = (MyPoint)o;		double d1 = Math.sqrt(x*x + y*y);		// Frankly, it is safe to skip theMath.sqrt but it is done		double d2 = Math.sqrt(p.x*p.x + p.y*p.y);	// for the sake of the concept		if (d1 < d2)		{			return -1;			// Orig point goes/stays before the point it is compared to		}		else if (d2 < d1)		{			return 1;			// Orig point goes after the point it is compared to		}			return 0;			// Points stay put.	}}class SortPoints{	public static void main(String[] args)	{		Random rnd = new Random();		MyPoint[] points = new MyPoint[10];		for (int i=0; i
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.

Sitemap