Using comparable to set the order of Objects in TreeSet
* Here we are creating a class Contact and each object of this class will represent contact of a person.
* In TreesetDemo1 class, we will create multiple objects of this class with different values of variables.
* After creating objects, we will add them to TreeSet and test their order.
*
* Here we are implementing Com parable interface to the class that will provide object to be placed into ordered way into TreeSet.
* It has only single method compareTo(OtherObject obj) that will return int value.
* The Object that implements Comparable interface properly, is placed into ordered way automatically.
package collection.demos;
import java.util.TreeSet;
class Contact implements Comparable<Contact>{
String name;
String email;
long phone;
public Contact(String name, String email, long phone) {
this.name = name;
this.email = email;
this.phone = phone;
}
public String toString() {
return name+"\t"+email+"\t"+phone;
}
public int compareTo(Contact o) {
int diff=name.compareTo(o.name);
return diff;
}
}
public class TreeSetDemo2 {
public static void main(String[] args) {
TreeSet<Contact> contacts=new TreeSet<Contact>();
Contact c1=new Contact("zyan","zyan@gmail.com",123467);
Contact c2=new Contact("corb","cord@gmail.com",534544);
Contact c3=new Contact("gogle","gogle@gmail.com",33444);
Contact c4=new Contact("sraf","straf@gmail.com",255346);
contacts.add(c1);
contacts.add(c2);
contacts.add(c3);
contacts.add(c4);
for (Contact c : contacts) {
System.out.println(c);
}
}
}
/*
OUTPUT
corb cord@gmail.com 534544
gogle gogle@gmail.com 33444
sraf straf@gmail.com 255346
zyan zyan@gmail.com 123467
*/
Subscribe to:
Post Comments (Atom)
Popular Posts
-
HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very ...
-
If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLD...
-
HTML Page < html > < head > < title > welcome </ title > < script language = "javascript"...
-
In web application ApplicationContext is created using Context Loaders. there are two implementations of context loader. ContextLoaderLis...
-
JSP code <%@page import="java.sql.*"%> <%! int prod_id=1; String prod_name="Laptop"; int qty=2; float p...
-
Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values ...
-
HSQLDB database it a very small database that can be controlled from you application. you can start and stop the database by writing the ...
-
StudentRegistration │ index.html │ login.html │ └───WEB-INF │ web.xml │ └───classes login.class lo...
-
package process; import javax.swing. * ; import javax.swing.table. * ; import java.sql. * ; /** * This class create JTable from Da...
-
Hibernate ORM framework does not provide any approch to connect to MS Access database. Unfortunately, Access is not supported officially...
1 comment:
Thanx for the Code I was Stuck in middle of program.
Post a Comment