Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Comparator;
- class Comp {
- public static void main(String[] args) {
- System.out.println(max2(1,2));
- Person p1 = new Person(11);
- Person p2 = new Person(20);
- System.out.println(max2(p1,p2));
- System.out.println(max2(p1,p2,new PersonComp()));
- System.out.println(max2(p1,p2,new Comparator<Person>(){
- public int compare(Person a, Person b) {
- return b.alder - a.alder;
- }
- }));
- }
- public static <T extends Comparable<T>> T max2(T a, T b) {
- if (a.compareTo(b) > 0) return b;
- else return a;
- }
- public static <T> T max2(T a, T b, Comparator<T> comp) {
- if (comp.compare(a,b) > 0) return b;
- else return a;
- }
- }
- class Person implements Comparable<Person>{
- int alder;
- Person(int a) {
- alder = a;
- }
- public String toString() {
- return alder+"";
- }
- public int compareTo(Person p) {
- return alder - p.alder;
- }
- }
- class PersonComp implements Comparator<Person> {
- public int compare(Person a, Person b) {
- return (a.alder % 10) - (b.alder % 10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement