August 31, 2012 at 12:52 PM
public class Employee{
int empId;
String eName;
public Employee(int empId, String eName) {
this.empId = empId;
this.eName = eName;
}
@Override
public String toString() {
return "ID:"+empId+"NAME:"+eName;
}
}
public class Student {
String sName;
String sDept;
public Student(String sName, String sDept) {
this.sName = sName;
this.sDept = sDept;
}
@Override
public String toString() {
return "NAME:"+sName+"DEPT:"+sDept;
}
}
import java.util.Comparator;
public class CommonSort implements Comparator{
@Override
public int compare(Object obj1, Object obj2) {
int i=0;
if((obj1!=null && obj2!=null) && obj1 instanceof Employee && obj2 instanceof Employee){
Employee e1=(Employee)obj1;
Employee e2=(Employee)obj2;
i=e1.eName.compareTo(e2.eName);
System.out.println(e1.eName+"--"+e2.eName+"--"+i);
}
if((obj1!=null && obj2!=null) && obj1 instanceof Student && obj2 instanceof Student){
Student s1=(Student)obj1;
Student s2=(Student)obj2;
i=s1.sDept.compareTo(s2.sDept);
}
return i;
}
}
import java.util.Iterator;
import java.util.TreeSet;
public class EmpMain {
/**
* @param args
*/
public static void main(String[] args) {
CommonSort cs=new CommonSort();
TreeSet<Employee> ts =new TreeSet<Employee>(cs);
ts.add(new Employee(123,"vijay"));
ts.add(new Employee(98,"kumar"));
ts.add(new Employee(100,"abhi"));
ts.add(new Employee(50,"dhruv"));
ts.add(new Employee(150,"sasi"));
for (Iterator iterator = ts.iterator(); iterator.hasNext();) {
Employee employee = (Employee) iterator.next();
System.out.println(employee);
}
System.out.println("-----------------------------------");
TreeSet<Student> ts2 =new TreeSet<Student>(cs);
ts2.add(new Student("kumar","cse"));
ts2.add(new Student("abhi","it"));
ts2.add(new Student("vijay","mec"));
ts2.add(new Student("bhavani","ece"));
ts2.add(new Student("sasi","eee"));
for (Iterator iterator = ts2.iterator(); iterator.hasNext();) {
Student student = (Student) iterator.next();
System.out.println(student);
}
}
}
Related Tutorials/Questions & Answers:
Java Comparator ExampleJava Comparator Example Can you provide me an
example of
Comparator Interface?
A
comparator object is capable of comparing two different... is an
example that compares the object of class Person by age.
import java.util.
comparatorcomparator what is a
comparator
Hi Friend,
A
comparator is an interface that can be used to create objects to pass to sort methods or sorting data structures. A
Comparator must define a compare function which takes
Advertisements
ModuleNotFoundError: No module named 'comparator'ModuleNotFoundError: No module named '
comparator' Hi,
My Python... '
comparator'
How to remove the ModuleNotFoundError: No module named '
comparator' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'comparator'ModuleNotFoundError: No module named '
comparator' Hi,
My Python... '
comparator'
How to remove the ModuleNotFoundError: No module named '
comparator' error?
Thanks
Hi,
In your python environment you
Example of HashSet class in javaExample of HashSet class in
java.
In this part of tutorial, we... unique. You can not store duplicate value.
Java hashset
example.
How....
Example of Hashset iterator method in
java.
Example of Hashset size() method
What is array in java with example?What is array in
java with
example? Hi,
I am beginner in
Java and want to learn
Java Array concepts. Which is the best tutorials for learn
Java Array with
example codes?
What is array in
java with
example?
Thanks
Java FTP Client ExampleJava FTP Client Example How to write
Java FTP Client
Example code?
Thanks
Hi,
Here is the
example code of simple FTP client in
Java which downloads image from server FTP Download file
example.
Thanks
Example of HashMap class in javaExample of HashMap class in
java.
The HashMap is a class in
java collection framwork. It stores values in the
form of key/value pair. It is not synchronized
Java Collection : TreeSet ExampleJava Collection : TreeSet
Example
This tutorial contains description of TreeSet with
example.
TreeSet :
TreeSet class is defined in java.util... or by the
comparator. Since it provides
fast access and fast retrieval so
FTP Java exampleFTP
Java example Where is the FTP
Java example on your website? I am... examples of FTP at: FTP Programming in
Java tutorials with
example code.
Thaks... functionality in my
Java based application.
My application is swing based and I have
freemarker example - Java Beginners an
example for freemarker. i want to get the values from
java and display those values in the page designed using freemarker(how to get the values from
java). and please provide an
example with code and directory structure. send me ASAP
Example Code - Java BeginnersExample Code I want simple Scanner Class
Example in
Java and WrapperClass
Example.
What is the Purpose of Wrapper Class and Scanner Class .
when i compile the Scanner Class
Example the error occur :
Can not Resolve symbol
Java FTP ExampleJava FTP Example Is there any
java ftp
example and tutorials... and tutorials that teaches you how to user FTP in your
Java project.
Most commonly used FTP api in
java is Apache FTP.
Browse all the FTP tutorials at
Java FTP
java string comparison examplejava string comparison example how to use equals method in String... strings are not same.
Description:-Here is an
example of comparing two strings using equals() method. In the above
example, we have declared two string
Java nested class exampleJava nested class example Give me any
example of Nested Class.
Nested Class: Class defined within another class is called nested class... class.
Example:
public class NestedClass{
private String outer = "Outer
throws example program javathrows
example program java how to use throws exception in
java?
The throws keyword is used to indicate that the method raises..." java.lang.ArithmeticException: / by zero
Description:- Here is an
example of throws clause. We
exampleexample
example on Struts framework
exampleexample
example on Struts framework
Java Comparable ExampleJava Comparable Example I want to know the use of Comparable Interface. Please provide me one
example
Comparable interface is used....
Here is an
example that compares two ages using Comparable Interface.
import
Java hashset example.Java hashset
example.
HashSet is a collection. You can not store duplicate value in
HashSet.
In this
java hashset exmple, you will see how to create HashSet in
java
application and how to store value in Hashset
Java collection Stack example :- -1
Description:- The above
example demonstrates you the Stack class in
java...
Java collection Stack example How to use Stack class in
java.... Here is an
example of Stack class.
import java.util.Stack;
public class
Inheritance java ExampleInheritance
java Example How can we use inheritance in
java program... for bread
Description:- The above
example demonstrates you the concept... properties of the superclass. In the given
example, the class Animal is a superclass
Java HashMap example.Java HashMap
example.
The HashMap is a class in
java. It stores values in name values pair. You can
store null value of key and values.
Here... of map.
Code:
HashMapExample .
java
(adsbygoogle
Java Map ExampleJava Map Example How we can use Map in
java collection?
The Map interface maps unique keys to value means it associate value to unique...
Description:- The above
example demonstrates you the Map interface. Since Map
Java File Management Example of file.
Read the
example Read file in
Java for more information on reading...
Java File Management Example Hi,
Is there any ready made API in
Java for creating and updating data into text file? How a programmer can write code
exampleexample i need ex on struts-hibernate-spring intergration
example
Struts Spring Hibernate Integration
printing example - Java Beginnersprinting example Is it possible to print
java controls using print method?
My problem is to print a student mark list using
java?
The mark list should like that of university mark list