| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This repository contain all the Java 8 related concepts and examples.
Java 8 introducted 4 Type of Interfaces and any lambda function written is adhered to this interface.
1.Consumer Interface
This interface only passes parameters and no return value.
Consumer<T> consumer=new Consumer<T>() {
@Override
public void accept(Student t) {
// TODO Auto-generated method stub
}
};Consumer<T> consumer=((T s)->{
System.out.println(s);
});Methods:
2.BiConsumer : is similar to the consumer but user 2 parameters.
3.Supplier Interface
This interface only returns value.
Supplier<T> supplier=new Supplier<T>()
@Override
public Student get() {
// TODO Auto-generated method stub
return null;
}
};Supplier<Student> supplier=(()->{
return null;
});4.Predicate
This interface is used to validate the input and returns boolean only
Predicate<T> predicate=new Predicate<T>() {
@Override
public boolean test(T t) {
// TODO Auto-generated method stub
return false;
}
};Predicate<T> predicate=((T t)->{
return false;
});Methods:
and : compares 2 predicate expression with and
or : compares 2 predicate expression with or
negate : inverse operation on the expression result
5.BiPredicate : Similar to the Predicate difference it uses 2 parameters.
6.Functions:
This is used when there is a parameter sent and there is a return value.
Function<Student,Student> function=new Function<Student, Student>() {
@Override
public Student apply(Student t) {
// TODO Auto-generated method stub
return null;
}
};Function<Student, Student> fucn = ((Student a) -> {
return null;
});>> andThen : combines function with another function.
7.BiFunction : is similar to the function interface difference is it take 2 parameters.
8.UnaryOperator : is similar to the function interface difference is the data type of the parameter send and returned are same.
9.BinaryOperator : is similar to the UnaryOperator interface difference is it takes 2 parameter and returns one value with all same data type.
1.Static Reference.
Test::printName , where printName is a static method
is similar to
(T t)->{
Test.printName(t)
}
2.Instance Reference.
test::printName , where test is an object
is similar to
(T t)->{
test.printName(t)
}
3.Class Reference
Student::printValue , where Student is the name of the class, printValue is the method name
is similar to
(Student s)->{
s.printValue()
}
4.Constructor Reference
public static void constructorReferenceObject3() {
BiFunction<String, String, Test> biFunc = Test::new;
Test test=biFunc.apply("testI", "testJ");
System.out.println("I and J value");
System.out.println(test.getI());
System.out.println(test.getJ());
}
public static void constructorReferenceObject2() {
Function<String, String> strFunc = String::new;
String str = strFunc.apply("hello user");
System.out.println(str);
}
public static void constructorReferenceObject1() {
Supplier<String> strFunc = String::new;
String str = strFunc.get();
System.out.println(str);
}
Boxing : int -> Integer.
Unboxing : Integer -> int.
IntStream:
* mapToLong : will convert to LongStream.
* mapToDouble : will convert to DoubleStream.
* boxed : will convert intStream to Stream<Integer>.
* mapToObj: will convert to Stream<Object>.The terminal operations are used to finalize the stream process and helps in fetching the result.
All the below methods belongs to Collectors class and is used with stream.collect().
joining : concats the streams of string values, It has 3 variations - joining , joining(delimiter) , joining(delimiter, prefix , suffix).
counting : return the total number for entries.
mapping : would perform a transformation and then collects the values.
maxBy / minBy : uses a comparator and returns Max / Min value.
Sum : perform sum value
Avg : perform average value
groupBy - 3 variations exists
paritioningBy: perform classification by true/false
Used to avoid null pointer and to avoid having too many null conditions.
Note : Precedence order if a method is overridden in the child and is invoked then the child takes the precedence over the parent.
| Back | FazBrowse Home | New Git URL |