Baby steps of util in the Java framework. This writeup is just a check point what I liked this week using these packages.
Consider the case where you have to build out a fact tree where you will have to sort the students in your class based on their birth dates. Treemap gives a natural way of sorting based on date of birth. I liked the simplicity of the TreeMap.
package commons;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapSample {
public static void main(String args[]) {
Kid kids[] = {new Kid("Kid1", DateTime.parse("05/01/05", DateTimeFormat.forPattern("MM/dd/yy")).toLocalDate()),
new Kid("Kid2", DateTime.parse("01/06/05", DateTimeFormat.forPattern("MM/dd/yy")).toLocalDate()),
new Kid("Kid3", DateTime.parse("07/12/12", DateTimeFormat.forPattern("MM/dd/yy")). toLocalDate()) };
Map sortedMap = new TreeMap();
for (Kid kid : kids) {
sortedMap.put(kid.getDob(),kid.getName());
}
System.out.println(sortedMap.values());
}
static class Kid {
private String name;
private LocalDate dob;
public Kid(String name, LocalDate dob) {
this.name = name;
this.dob = dob;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
}
}