How to use Comparable and Comparator Interfaces in Java
package com.nano.test;
import java.util.*;
class Employee implements Comparable {
int EmpID;
String Ename;
double Sal;
static int i;
public Employee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;
}
public Employee(String ename, double sal) {
EmpID = i++;
Ename = ename;
Sal = sal;
}
public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal" + Sal;
}
public int compareTo(Object o1) {
if (this.Sal == ((Employee) o1).Sal)
return 0;
else if ((this.Sal) > ((Employee) o1).Sal)
return 1;
else
return -1;
}
}
public class ComparableDemo{
public static void main(String[] args) {
List ts1 = new ArrayList();
ts1.add(new Employee ("AaAa",40000.00));
ts1.add(new Employee ("aAaA",20000.00));
ts1.add(new Employee ("AaA",50000.00));
ts1.add(new Employee ("aAa",70000.00));
Collections.sort(ts1);
Iterator itr = ts1.iterator();
while(itr.hasNext()){
Object element = itr.next();
System.out.println(element + "\n");
}
String line = "this is a sentence hai";
String lastWord = line.substring(line.lastIndexOf(' ') + 1);
System.out.println("Lastword:"+lastWord);
ComparableDemo cb= new ComparableDemo();
int rn =cb.reverseNumber(4322);
System.out.println("ReverseNumber"+rn);
}
int reverseNumber(int number)
{
int reversedNumber = 0;
while(number != 0)
{
reversedNumber = (reversedNumber * 10) + (number % 10);System.out.println("red"+reversedNumber);
number /= 10;
}
return reversedNumber;
}
}
My Favorite Site's List
-
I Read 20+ Coding Books : Here Are My Top 10 Recommendations
-
Hello guys, Coding is one of the most essential skills for programmers, and
if you want to survive as a programmer, you must improve your coding ability.
G...
1 month ago
-
How to Initialize an Array in Java
-
Connect to us ( @twitter | @facebook )
To initialize an array in Java, use square brackets, curly braces, or the
built-in methods of the IntStream interf...
1 year ago
-
Book Review: The Girl in White Gloves by Kerri Maher
-
[image: The Girl in White Gloves]*The Girl in White Gloves by Kerri Maher*
*Goodreads*
I was curious to learn about old Hollywood, but beyond that I wasn't ...
6 years ago
-
convert .pem file to .ppk using puttygen - AWS
-
PEM-Privacy Enhanced Mail is a Base64 encoded DER certificate file
format. PEM certificates are frequently used for web servers as they can
easily be tra...
6 years ago
-
Top Developer Collaboration Tools
-
How to drive your project into a corner? Just in case you wondered, there
are multiple options. The surest one is miscommunication. Considering that
you ca...
7 years ago
-
Highlighting Google Cloud’s talks at Puppetize Live
-
Highlighting Google Cloud’s talks at Puppetize Live
Kristina Psaris-Weis 16 October 2018
Off
[image: Andrew Nhem]
Andrew Nhem
Events — Partn...
7 years ago
-
Meet The Wattpad Stars: Brittany Geragotelis
-
After 10 years of pounding the pavement with publishers and agents,
Brittany Geragotelis decided to break free from gatekeepers by posting an
original stor...
9 years ago
-
Breaking Down Barriers in Sexual and Reproductive Health Reporting in Africa
-
*This is a guest post by Humphrey Nabimanya, founder of Reach a Hand
Uganda. *
[image: 2016-04-15-1460736651-1435623-huffpo1.jpg]*Journalists and bloggers...
10 years ago
-
Wildfly 8.x : Control maximum number of connections (threads) assigned to
an application
-
In Wildfly, multiple applications can be deployed together. With the
default configuration, there is no control on the maximum number of
connections/threa...
10 years ago
-
OSGi - Road Ahead
-
OSGi has been introduced many times in the past few years and will still
require some in the future1. With so many introductions, one might be
inclined to ...
12 years ago
-
List of demonstrations on this page And How To Find Them And How To Get
Them.
-
Here Is A List of demonstrations on this page with the link back to the
tutorial :
How To Add A Scrolling Recent Posts Gadget To Your Blog
You Can See T...
16 years ago
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#update below script more than 500 posts
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.