Learn how to give your classes a “natural order”. A tutorial on ordering your objects in TreeSets and TreeMaps and with Collections.sort() for Lists, using the Comparable Interface. In this tutorial I also take a look at the Collection interface and the SortedSet interface.
After starting the video, click the maximise button to make it fullscreen so you can see the code!
Code For This Tutorial
App.java:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
class Person implements Comparable<Person> {
private String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Person person) {
int len1 = name.length();
int len2 = person.name.length();
if(len1 > len2) {
return 1;
}
else if(len1 < len2) {
return -1;
}
else {
return name.compareTo(person.name);
}
}
}
public class App {
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
SortedSet<Person> set = new TreeSet<Person>();
addElements(list);
addElements(set);
Collections.sort(list);
showElements(list);
System.out.println();
showElements(set);
}
private static void addElements(Collection<Person> col) {
col.add(new Person("Joe"));
col.add(new Person("Sue"));
col.add(new Person("Juliet"));
col.add(new Person("Clare"));
col.add(new Person("Mike"));
}
private static void showElements(Collection<Person> col) {
for(Person element: col) {
System.out.println(element);
}
}
}
Joe Sue Mike Clare Juliet Joe Sue Mike Clare Juliet
Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.
All pages on this site are copyright © 2013 John W. Purcell
Thanks very much for your tutorials, I have been working in and out of Java development for a number of years and much of my coding has revolved around asking others, googling answers and looking at code, and not really understanding or appreciating things.
I like your style I even purchased your Servlets course as I was that impressed.
I was looking forward to the queue tutorial
Thanks for purchasing!! Keeps a roof over my head
Maybe I will actually make the queue tutorial then …..
Dear Sir, I am really glad to find your tutorials on net and I am a learner. They are very useful. Here I have a doubt in the above program. If there is another variable Integer number in the Person class and if want to implement the compareTo method, how should I write, I can confused with the equals method and compareTo method. Please expain !!
Thank you very much..
Hi, the thing to keep in mind is just that you must return 1 from compareTo if the current objecct is greater than the one it’s being compared to, or -1 if it is less. It’s up to you to decide what difference another integer makes. You might decide that you’ll check the extra integer only if all other information is equal, for example.
Continue : Is it really meaningful to compare two objects based on two variables? If yes how to achieve it through this way?
It’s up to you to decide. I can’t give you source code, but it’s just standard programming plus common sense. Thing of a first name and last name, for example … we often use both to determine the alphabetical order of a name. We look at the first name only if the last name is the same.