Generics Functional Interface

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

// import java.util.

Scanner;
import java.util.function.*;

public class Main {

public static void main(String args[]) {


Sample<Integer, String> s = new Sample<Integer, String>();
Sample<String, String> s1 = new Sample<String, String>();
s.setVar1(0);
s.setVar2("A");
System.out.println(s.getVar1());
System.out.println(s.getVar2());

s1.setVar1("Bryan");
s1.setVar2("Vern");
System.out.println(s1.getVar1());
System.out.println(s1.getVar2());

OtherExample<Boolean, Integer> oe = new OtherExample<Boolean, Integer>();


oe.sample1(10, 25);
SampleInterface<Boolean> si = a -> {
// return a;
};

si.show(true);

BiConsumer<String, String> consume = (a, b) -> System.out.println(a + " " + b);


Supplier<Integer> supply = () -> {
return 100;
};
Predicate<Integer> predict = a -> {
return a >= 18;
};
BiPredicate<Integer, Integer> compare = (a, b) -> {
if (a > b) return true;
return false;

};
BiFunction<Integer, Integer, String> bifunction = (a , b) -> { return a + " " +
b; };
UnaryOperator<Integer> unary = a -> { return a * a; };
BinaryOperator<Integer> binary = (a, b) -> { return a + b; };
consume.accept("Bi","Consume");
System.out.println( supply.get() );
if ( predict.test( 18 ) ) {
System.out.println("You are an adult");
} else {
System.out.println("You are not an adult");
}
System.out.println( bifunction.apply(10, 25) );
System.out.println( unary.apply(10) );
System.out.println( binary.apply(50, 25) );
}
}

@FunctionalInterface
interface SampleInterface<T> {
void show(T a);
}
// primitive
// int, short, byte, char, boolean, double, float, long

// reference
// String, Integer, Double, Character, Boolean, Long, Short, Byte, Float,
// String, Class, Interface

// Generics
class OtherExample<A, B> {
A sample1;
B sample2;

A sample1(int a, int b) {
return sample1;
}

B sample2() {
return sample2;
}
}

class Sample<T, D> {


private T var1;
private D var2;

public void setVar1(T var1) {


this.var1 = var1;
}

public T getVar1() {
return var1;
}

public void setVar2(D var2) {


this.var2 = var2;
}

public D getVar2() {
return var2;
}

You might also like