Commit a600359b authored by Pinky Sabu's avatar Pinky Sabu

Java 8 POCS [Pinky Sabu]

parent d2373eec
import domain.Domain;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
public class BinaryOperatorExamples {
public static void main(String[] args) {
biFunction();
binaryOperator();
}
private static void binaryOperator() {
BinaryOperator<Integer> binary = (x, y) -> x + y;
binary.apply(2, 3);
}
private static void biFunction() {
BiFunction<Integer, Integer, Integer> result = (x, y) -> x + y;
System.out.println(result.apply(2, 3));
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class ConsumerExamples {
public static void main(String[] args) {
simpleConsumer();
biConsumer();
inbuiltBiConsumer();
}
private static void inbuiltBiConsumer() {
Map<String,String> map=new HashMap<>();
map.put("1","1");
map.put("2","2");
map.forEach((x,y)->System.out.println("inbuiltBiConsumer"+x.concat(y)));
}
private static void biConsumer() {
BiConsumer<String, String> biConsumer = (x, y) -> x.concat(y);
System.out.println(biConsumer);
}
private static void simpleConsumer() {
Consumer<String> consumer = x -> System.out.println(x);
consumer.accept("Hello ");
}
}
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import java.util.function.Function;
public class FunctionExamples {
public static void main(String[] args) {
simpleFunction();
chainFunction();
biFunction();
biFunctionWithFunction();
}
private static void biFunctionWithFunction() {
BiFunction<Integer, Integer, Integer> biFunction = (x, y) -> x + y;
Function<Integer, String> function = x -> "Hello".concat(String.valueOf(x));
System.out.println(" biFunctionWithFunction :" + biFunction.andThen(function).apply(2, 3));
}
private static void biFunction() {
BiFunction<Integer, Integer, List<Integer>> biFunction = (x, y) -> Arrays.asList(x + y);
System.out.println(biFunction.apply(2, 3));
}
private static void chainFunction() {
Function<String, Integer> function1 = x -> x.length();
Function<Integer, Integer> function2 = x -> x * 3;
System.out.println(function1.andThen(function2).apply("Hello Test"));
}
private static void simpleFunction() {
Function<String, Integer> function = x -> x.length();
System.out.println(function.apply("Test"));
}
}
import domain.Domain;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PredicateExamples {
public static void main(String[] args) {
predicate();
predicateAnd();
predicateOr();
predicateNegate();
predicateChaining();
biPredicate();
biPredicateAsFunctionArgument();
}
private static void biPredicateAsFunctionArgument() {
List<Domain> domainList = Arrays.asList(new Domain("google.com", 1), new Domain("i-am-spammer.com", 10),
new Domain("mkyong.com", 0), new Domain("microsoft.com", 2));
BiPredicate<String, Integer> biPredicate = (x, y) -> {
return (x.equals("google.com") || y == 0);
};
System.out.println(domainList
.stream()
.filter(domain -> biPredicate.test(domain.getName(), domain.getScore()))
.collect(Collectors.toList()));
}
private static void biPredicate() {
BiPredicate<String, Integer> biPredicate = (x, y) -> x.length() == y;
System.out.println(biPredicate.test("Bat", 3));
}
private static void predicateChaining() {
Predicate<String> startsWithA = x -> x.startsWith("A");
Predicate<String> startsWithB = x -> x.startsWith("B");
System.out.println(startsWithA.or(startsWithB).test("Bat"));
}
private static void predicateNegate() {
List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
Predicate<String> startsWith = x -> x.startsWith("A");
System.out.println(list.stream().filter(startsWith.negate()).collect(Collectors.toList()));
}
private static void predicateOr() {
List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
Predicate<String> startsWith = x -> x.startsWith("A");
Predicate<String> length = x -> x.length() > 2;
System.out.println(list.stream().filter(startsWith.or(length)).collect(Collectors.toList()));
}
private static void predicate() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
Predicate<Integer> noGreaterThan = x -> x > 5;
System.out.println(">>" + list.stream().filter(noGreaterThan).sorted().collect(Collectors.toList()));
}
private static void predicateAnd() {
List<Integer> list = Arrays.asList(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
Predicate<Integer> greaterThan = x -> x > 10;
Predicate<Integer> lessThan = x -> x < 18;
System.out.println(list.stream().filter(greaterThan.and(lessThan)).collect(Collectors.toList()));
}
}
import domain.Domain;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.function.Supplier;
public class SupplierExamples {
public static void main(String[] args) {
simpleExample();
supplierToReturnObject();
}
private static void supplierToReturnObject() {
System.out.println(factory(Domain::new));
System.out.println(factory(() -> new Domain("hi", 10)));
}
private static Domain factory(Supplier<? extends Domain> s) {
Domain domain = s.get();
if(!Optional.ofNullable(domain.getName()).isPresent()) {
domain.setName("Hello");
domain.setScore(100);
}
return domain;
}
private static void simpleExample() {
Supplier<LocalDateTime> supplier = () -> LocalDateTime.now();
System.out.println(supplier.get());
}
}
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class UnaryOPeratorExamples {
public static void main(String[] args) {
function();
unaryOperator();
}
private static void unaryOperator() {
UnaryOperator<Integer> data = x -> x * 2;
System.out.println(data.apply(2));
}
private static void function() {
Function<Integer, Integer> function = x -> x * 2;
System.out.println(function.apply(2));
}
}
package domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Domain {
String name;
Integer score;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment