Last active
August 29, 2015 14:13
-
-
Save bobbyjam99-zz/349a7a598ad87f508c44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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