import java.util.function.*; /* interface Comparable { int compareTo(T x); } interface Function { B apply(A x); } interface Consumer { void accept(A); } interface Supplier { A get(); } Function f = x -> Math.sqrt(x); // can instantiate with lambda */ class person implements Comparable { public String name; public int compareTo(person x) {return name.compareTo(x.name);} } class student extends person implements Comparable { public double gpa; // boring stuff skipped // How is the compareTo method be inherited? } //class Sorter> // older form class Sorter> // since jdk 1.8 { void bubblesort(T[] A) // bubble sorts (pointer to) array A { for(int i=0;i0) { T tmp = A[k]; A[k]=A[k+1]; A[k+1]=tmp; //swap }// not efficient but that's not the point here }//bubblesort static Function compose(Function f, Function g) { return x -> f.apply(g.apply(x)); } static void consume_twice(Consumer consumer, A x) { consumer.accept(x); consumer.accept(x); } static void notreallymain() { Sorter S = new Sorter(); //compiles with ? super T //Sorter S = new Sorter(); //classes never covariant Function f = x -> Math.sqrt(x); Function g = x -> (int)x; var fog = compose(f,g); System.out.println(fog.apply('a')); consume_twice(x -> System.out.println(x), "abc"); } } // what if I try to do a super on a return type? -- following won't compile /* interface D { T f(); //void f(T x); } class DD implements D
{ public DD f() { return null; } //public void f(DD x) {} } class CC { T g(D d) { return d.f(); } } */