Skip to content

Instantly share code, notes, and snippets.

@bobbyjam99-zz
Last active August 29, 2015 14:13
Show Gist options
  • Save bobbyjam99-zz/349a7a598ad87f508c44 to your computer and use it in GitHub Desktop.
Save bobbyjam99-zz/349a7a598ad87f508c44 to your computer and use it in GitHub Desktop.
import java.util.Comparator;
import java.util.NavigableSet;
import java.util.TreeSet;
class Employee {
private final String name;
Employee(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
class NameComparator implements Comparator<Employee> {
@Override
public int compare(Employee x, Employee y) {
return x.getName().compareTo(y.getName());
}
}
public class EmployeeStorer {
public static void main(String... args) {
NavigableSet<Employee> employees = new TreeSet<>(new NameComparator());
employees.add(new Employee("Kanako"));
employees.add(new Employee("Shiori"));
employees.add(new Employee("Ayaka"));
employees.add(new Employee("Momoka"));
employees.add(new Employee("Reni"));
employees.forEach(e -> System.out.println(e.getName()));
}
}
import java.util.NavigableSet;
import java.util.TreeSet;
class Employee {
private final String name;
Employee(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class LambdaEmployeeStorer {
public static void main(String... args) {
NavigableSet<Employee> employees = new TreeSet<>(
(x, y) -> x.getName().compareTo(y.getName())
);
employees.add(new Employee("Kanako"));
employees.add(new Employee("Shiori"));
employees.add(new Employee("Ayaka"));
employees.add(new Employee("Momoka"));
employees.add(new Employee("Reni"));
employees.forEach(e -> System.out.println(e.getName()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment