Java8 New Features - Study Guide
Java8 New Features - Study Guide
Author
Srinivas Dande
1)Animal.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface Animal {
public abstract void eating();
public abstract void sleeping();
@Override
public void eating() {
System.out.println("Dog is eating");
}
@Override
public void sleeping() {
System.out.println("Dog is sleeping");
}
@Override
public void running() {
System.out.println("Dog is running");
}
}
3)Cat.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Cat implements Animal{
@Override
public void eating() {
System.out.println("Cat is eating");
}
@Override
public void sleeping() {
System.out.println("Cat is sleeping");
}
@Override
public void thinking() {
System.out.println("Cat is thinking");
}
}
1)A.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
3)Hello.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A,B{
@Override
public void show() {
System.out.println("Hello- show() ");
}
public void test() {
System.out.println("Hello- test() ");
show();
A.super.show();
B.super.show();
}
}
4)Demo1.java
package com.jlcindia.demo2;
1)A.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
/*
default boolean equals(Object obj) {
System.out.println("A- equuals() ");
}
*/
}
2)Hello.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A{
1)A.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
default void m1() {
System.out.println("A- m1() ");
}
}
2)B.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface B extends A {
default void m2() {
System.out.println("B- m2() ");
m1();
}
}
4)Demo4.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
Hello hello=new Hello();
hello.m1();
hello.m2();
}
}
1)A.java
package com.jlcindia.demo5;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
default void m1() {
System.out.println("A- m1() ");
}
}
3)Hello.java
package com.jlcindia.demo5;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements B{
4)Demo4.java
package com.jlcindia.demo5;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
Hello hello=new Hello();
hello.m1();
hello.m2();
}
}
Q6) Can I mark the Overriden Default Methods as Default in the Sub Class?
Ans:
Q7) Can I Override Object class methods as Default Methods inside Interface?
Ans:
Q9) What happens when Sub Class is implementing two interfaces which are having same default
method?
Ans:
Q12) Can I call one Default Method from another Default Methods of same Interface?
Ans:
Q13) Can I Override Interface Default Method extended from other Interface?
Ans:
Concrete methods (Methods with Body) Defined in the Interface with static keyword
are called as Static Methods.
Static Methods are public by default.
Static Methods will not be inherited to Sub classes.
Sub class can not override the Interface Static Methods.
If we write the Static Method of Interface in Sub Class then That will be treated as New
Method in Sub Class.
1)A.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
int P=101;
public final static int Q=102;
void m1();
public abstract void m2();
2)Hello.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A {
System.out.println(P); //Inherited
System.out.println(Q); //Inherited
m1(); //Overriden
m2(); //Overriden
m3(); //Overriden
A.super.m3();
m4(); //Inherited
A.super.m4();
A.m5();
A.m6();
//A.super.m6();
}
@Override
public void m1() {
System.out.println("Hello -m1");
}
@Override
public void m2() {
System.out.println("Hello -m2");
}
}
/*
@Override
public static void m5() {
System.out.println("Hello -m5");
}
*/
}
3)Demo1.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
public static void main(String[] args) {
Hello hello=new Hello();
hello.test();
}
}
1)A.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
static void m1() {
System.out.println("A - m1()");
}
}
3)Hello.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A,B {
m1();
A.m1();
B.m1();
}
Hello hello1=null;
hello1.show();
//2. Calling Static Method with Ref. Variable having Object address
//A aobj = new Hello();
//aobj.m1();
1)A.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
System.out.println("main method");
m1();
//m2(); // Can not call Instance Method in Static
Q7) What happens when Sub Class is implementing two interfaces which are having same Static
method?
Ans:
Q11) Can I call one Static Method from another Default Methods of same Interface?
Ans:
Q12) Can I call one Default Method from another Static Methods of same Interface?
Ans:
Functional Programming importance has been increasing in the recent days because it is
well suited for concurrent and event driven (or Reactive) programming.
That doesn’t mean that objects are bad Instead, the winning strategy is to blend object
oriented and functional programming
Lambda Expressions are introduced to support functional programming in Java
Lambda Expression is an anonymous functions
.i.e Lambda Expression is a function which doesn’t have the name, return type and access
modifiers
Lambda Expressions are used heavily inside the Collections, Streams libraries from Java 8
We need Functional interfaces to write lambda expressions.
1)Hello.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
void display();
2)Demo1.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
hello1.display();
hello1.m1();
//hello1.m2();
Hello.m2();
hello2.display();
hello2.m1();
//hello2.m2();
Hello.m2();
}
}
1)Hello.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
2)Demo2.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo2 {
hello1.display("Srinivas");
hello2.display("Sri");
hello3.display("Vas");
}
}
1)Hello.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
2)Demo3.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
hello2.test(95,45);
hello3.test(100,50);
hello4.test(95,45);
}
}
1)Hello.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
int test(int a,int b);
}
}
}
Q5) How Lambda Expressions are better than Annonymous Inner Classes?
Ans:
1)
Hello hello = (int a,int b) -> {
int sum = a + b;
return sum;
};
2)
Hello hello = (a, b) -> {
System.out.println( a + b);
};
3)
Hello hello = (a, b) -> {
return a + b;
};
4)
Hello hello = (a, b) -> a + b;
5)
Hello hello = (a, b,c) -> a + b-c;
6)
Hello hello = (int a, b) -> a + b;
7)
Hello hello = a, b -> a + b;
8)
Hello hello = (a, b) => {
return a + b;
};
Syntax:
Class::new
Ex:
Hello hello= Course::new;
1)Hello.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public int test(int a, int b);
}
2)MyInteger.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class MyInteger {
public static int findSum(int a, int b) {
return a + b;
}
}
3)Demo1.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
System.out.println("Done!!!");
}
}
1)Hello.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public int test(int a, int b);
}
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hai {
public void test(String str);
}
3)MyInteger.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
4)Demo2.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
}
}
1)Hello.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public Course test(int a,String b,String c,String d);
}
public Course() {
System.out.println("Course - 0 arg Con");
}
public Course(int courseId, String courseName, String duration, String trainer) {
System.out.println("Course - 4 arg Con");
this.courseId = courseId;
this.courseName = courseName;
this.duration = duration;
this.trainer = trainer;
}
//Setters and Getters
//toString() method
}
3)Demo3.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
//1.Lambda Style
Hello hello1 = (a, b, c, d) -> {
Course course = new Course(a, b, c, d);
return course;
};
System.out.println("Done!!!");
}
}
1)Hello.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public void test(int[] arr);
}
2)Demo4.java
package com.jlcindia.demo4;
import java.util.Arrays;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
//1.Lambda Style
hello1.test(myarr1);
hello2.test(myarr2);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
class Hello {
public static void show(int x) {
System.out.println(x);
}
}
public class Demo4 {
public static void main(String[] args) {
System.out.println("-------------------");
import java.util.ArrayList;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
class MyNumber {
public static boolean isEven(int number) {
if (number % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(int number) {
if (number % 2 != 0)
return true;
else
return false;
}
}
mylist.stream()
.filter(MyNumber::isEven)
.forEach(System.out::println);
System.out.println("-------------------");
mylist.stream()
.filter(MyNumber::isOdd)
.forEach(System.out::println);
System.out.println("-------------------");
mylist.stream()
.filter(a -> a % 2 == 0)
.forEach(a -> System.out.println(a));
System.out.println("-------------------");
Demo7.java
package com.jlcindia.demo7;
import java.util.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo6 {
public static void main(String[] args) {
mylist.stream()
.filter(a -> a % 2 == 0)
.map(a -> a* a)
.forEach(a -> System.out.println(a));
System.out.println("-------------------");
mylist.stream()
.filter(a -> a % 2 != 0)
.map(a -> a* a)
.forEach(a -> System.out.println(a));
}
}
1)Hello.java
package com.jlcindia.java8.demos;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
2)Demo1.java
package com.jlcindia.java8.demos;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
public static void main(String[] args) {
try {
int result = a/b;
System.out.println("Result is "+ result);
}catch(Exception ex) {
ex.printStackTrace();
}
hello.test(50, 0);
}
}
1)Hello.java
package com.jlcindia.java8.demos;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
void test(int a,int b) throws ArithmeticException;
}
2)Demo2.java
package com.jlcindia.java8.demos;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo2 {
public static void main(String[] args) {
System.out.println("main Begin");
//hello.test(50, 0);
try{
hello.test(50, 0);
}catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("main End");
}
}
Demo3.java
package com.jlcindia.java8.demos;
import java.util.function.Predicate;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
public static void main(String[] args) {
mybool = predicate1.test(28);
System.out.println(mybool);
Demo4.java
package com.jlcindia.java8.demos;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
System.out.println(mylist1);
mylist1.removeIf(predicate1);
System.out.println(mylist1);
System.out.println("-------------------------");
List<Integer> mylist2 = new ArrayList<>();
mylist1.add(20); mylist1.add(21); mylist1.add(22); mylist1.add(23);
mylist1.add(24); mylist1.add(25); mylist1.add(26);
Demo5.java
package com.jlcindia.java8.demos;
import java.util.ArrayList;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo5 {
public static void main(String[] args) {
System.out.println(mylist1);
mylist1.removeIf((num) -> num %2 ==0); //IMP
System.out.println(mylist1);
System.out.println("-------------------------");
List<Integer> mylist2 = new ArrayList<>();
mylist1.add(20); mylist1.add(21); mylist1.add(22); mylist1.add(23);
mylist1.add(24); mylist1.add(25); mylist1.add(26);
System.out.println(mylist2);
mylist2.removeIf((num) -> num %2 !=0); //IMP
System.out.println(mylist2);
}
}
Demo6.java
package com.jlcindia.java8.demos;
import java.util.function.Predicate;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo6 {
public static void main(String[] args) {
mybool = predicate2.test(99);
System.out.println(mybool);
mybool = predicate2.test(88);
System.out.println(mybool);
System.out.println("--------------");
mybool = predicate3.test(28);
System.out.println(mybool); //T
mybool = predicate3.negate().test(28);
System.out.println(mybool); //F
mybool = predicate4.test(19);
System.out.println(mybool); //T
mybool = predicate4.negate().test(19);
System.out.println(mybool); //F
}
}
Demo7.java
package com.jlcindia.java8.demos;
import java.util.function.Predicate;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo7 {
public static void main(String[] args) {
mybool = predicate1.and(predicate3).test(19);
System.out.println(mybool);
mybool = predicate2.and(predicate3).test(19);
System.out.println(mybool);
mybool = predicate1.or(predicate3).test(29);
System.out.println(mybool);
mybool = predicate1.or(predicate3).test(28);
System.out.println(mybool);
mybool = predicate1.and(predicate3).test(18);
System.out.println(mybool);
mybool = predicate2.or(predicate3).test(29);
System.out.println(mybool);
mybool = predicate2.or(predicate3).test(28);
System.out.println(mybool);
mybool = predicate2.and(predicate3).test(18);
System.out.println(mybool);
Interview Questions:
Q1) What is a Function Interface?
Ans:
Q10) Can we change the return type or parameters of SAM during implementation?
Ans:
Q11) How can I throws exception at method level using Functional Interface?
Ans:
1)
interface Hello {
}
2)
interface Hello {
void show();
}
3)
interface Hai {
void show();
}
}
4)
interface Hello {
void show();
default void display(){
System.out.println(“OK”);
}
5)
interface Hello {
void m1();
void m2();
}
Demo8.java
package com.jlcindia.demos;
import java.util.function.BiPredicate;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo8 {
public static void main(String[] args) {
}
}
Demo9.java
package com.jlcindia.demos;
import java.util.function.Function;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo9 {
public static void main(String[] args) {
Demo10.java
package com.jlcindia.demos;
import java.util.function.Function;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println(fun1.apply(5));
System.out.println(fun2.apply(5));
Demo11.java
package com.jlcindia.demo1;
import java.util.function.Function;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo11 {
public static void main(String[] args) {
UnaryOperator Functional Interface is sub type of Function which allows you specify only
One Type for both parameter and Return Value.
UnaryOperator Functional Interface
o Takes One Input Parameter
o Returns Output after processing.
UnaryOperator Functional Interface has the following methods.
Demo12.java
package com.jlcindia.demos;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo12 {
public static void main(String[] args) {
Demo13.java
package com.jlcindia.demos;
import java.util.function.BiFunction;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo13 {
public static void main(String[] args) {
BinaryOperator Functional Interface is sub type of BiFunction which allows you specify
only One Type for both parameter and Return Value.
BinaryOperator Functional Interface
o Takes Two Input Parameters
o Returns Output after processing.
BinaryOperator Functional Interface has the following methods.
Demo14.java
package com.jlcindia.demos;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo14 {
Demo15.java
package com.jlcindia.demos;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo15 {
public static void main(String[] args) {
consumer1.accept("Hello");
consumer1.accept("Srinivas");
consumer1.accept("Hai");
Demo16.java
package com.jlcindia.demos;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.function.Supplier;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo16 {
public static void main(String[] args) {
System.out.println(dow);
System.out.println(dow.getValue());
}
}
Optional is a container object which may or may not contain a non-null value.
Purpose of Optional class is to provide a type-level solution for representing optional values
instead of null references.
Optional class has the following methods.
T get();
boolean isPresent();
T orElse(T);
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
//Optional.empty() method
Optional<String> myopts= Optional.empty();
System.out.println("1. "+myopts);
System.out.println("2. "+myopts.orElse("Hello Guys"));
System.out.println("3. "+myopts);
System.out.println("4. "+myopts.isPresent());
//System.out.println("5. "+myopts.get());
if(myopts.isPresent()) {
System.out.println("5. "+myopts.get());
}else {
System.out.println("6. No value Found");
}
}
}
Demo2.java
package com.jlcindia.demos;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
//Optional.of() method
String str ="Srinivas";
Optional<String> myopts= Optional.of(str);
System.out.println("1. "+myopts);
System.out.println("2. "+myopts.orElse("Hello Guys"));
System.out.println("3. "+myopts);
System.out.println("4. "+myopts.isPresent());
//System.out.println("5. "+myopts.get());
if(myopts.isPresent()) {
System.out.println("5. "+myopts.get());
}else {
System.out.println("6. No value Found");
}
}
}
Demo3.java
package com.jlcindia.demos;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
}
}
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println("1. "+myopts);
System.out.println("2. "+myopts.orElse("Hello Guys"));
System.out.println("3. "+myopts);
System.out.println("4. "+myopts.isPresent());
if(myopts.isPresent()) {
System.out.println("5. "+myopts.get());
}else {
System.out.println("6. No value Found");
}
}
}
Demo5.java
package com.jlcindia.demos;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println("1. "+myopts);
System.out.println("2. "+myopts.orElse("Hello Guys"));
System.out.println("3. "+myopts);
System.out.println("4. "+myopts.isPresent());
if(myopts.isPresent()) {
System.out.println("5. "+myopts.get());
}else {
System.out.println("6. No value Found");
}
}
}
Demo6.java
package com.jlcindia.demos;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo6 {
public static void main(String[] args) {
//isPresent() Vs ifPresent()
String str =null;
//String str ="Srinivas";
Optional<String> myopts= Optional.ofNullable(str);
if(myopts.isPresent()) {
System.out.println(myopts.get());
}
if(myopts.isPresent()) {
System.out.println(myopts.get().toUpperCase());
}
System.out.println("-------------------");
System.out.println("Done!!!");
}
}
Demo7.java
package com.jlcindia.demos;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo7 {
public static void main(String[] args) {
//filter() method
String str1 =null;
Optional<String> myopts1 = Optional.ofNullable(str1);
System.out.println("Done!!!");
}
}
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo8 {
public static void main(String[] args) {
//map() method
String str1 ="Srinivas";
Optional<String> myopts1 = Optional.ofNullable(str1);
System.out.println("1. "+myopts1);
System.out.println("4. "+myopts4);
System.out.println("Done!!!");
}
}
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo9 {
public static void main(String[] args) {
System.out.println("1. "+myopts1);
System.out.println("2. "+myopts1.map(input -> input));
System.out.println("3. "+myopts1.flatMap(input -> input));
System.out.println("------------");
Optional<Optional<String>> x = myopts1.map(
input1 -> input1.map(input2 -> input2.toUpperCase())
);
System.out.println("x = "+x);
Optional<String> y = myopts1.flatMap(
input1 -> input1.map(input2 -> input2.toUpperCase())
);
System.out.println("y = "+y);
System.out.println("------------");
System.out.println("Done!!!");
}
}
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo10 {
public static void main(String[] args) {
System.out.println("1. "+myopts);
Optional<Optional<Optional<String>>> aa =
myopts.map(input1-> input1.map(input2->input2.map(input3-> input3.toUpperCase())));
System.out.println("2. "+aa);
Optional<Optional<String>> bb =
myopts.flatMap(input1-> input1.map(input2->input2.map(input3-> input3.toUpperCase())));
System.out.println("3. "+bb);
Optional<String> cc =
myopts.flatMap(input1-> input1.flatMap(input2->input2.map(input3->
input3.toUpperCase())));
System.out.println("4. "+cc);
System.out.println("Done!!!");
}
}
import java.util.NoSuchElementException;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println("1. "+myopts);
System.out.println("2. "+myopts.orElse("Hello Guys"));
System.out.println("3. "+myopts.orElseGet( () -> "Ok Guys"));
System.out.println("4. "+myopts.orElseThrow(NoSuchElementException::new));
System.out.println("1. "+myopts);
System.out.println("2. "+myopts.orElse("Hello Guys"));
System.out.println("3. "+myopts.orElseGet( () -> "Ok Guys"));
System.out.println("4. "+myopts.orElseThrow(NoSuchElementException::new));
}
}
Files Required:
1. Trainer.java 2. Course.java
3. Student.java 4. Demo1.java
5. Demo2.java
1) Trainer.java
package com.jlcindia.demos1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public Trainer() {}
2) Course.java
package com.jlcindia.demos1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Course {
private String courseName;
private double coursePrice;
private Trainer trainer;
3) Student.java
package com.jlcindia.demos1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public Student() {
}
// I have access to Only Student Object and want trainer Email and Phone
String trainerName = student1.getCourse().getTrainer().getTrainerName();
String trainerEmail = student1.getCourse().getTrainer().getTrainerEmail();
String trainerPhone = student1.getCourse().getTrainer().getTrainerPhone();
System.out.println(trainerName);
System.out.println(trainerEmail);
System.out.println(trainerPhone);
System.out.println("-----------------------------");
// I have access to Only Student Object and want trainer Email and Phone
trainerName = student2.getCourse().getTrainer().getTrainerName();
trainerEmail = student2.getCourse().getTrainer().getTrainerEmail();
trainerPhone = student2.getCourse().getTrainer().getTrainerPhone();
System.out.println(trainerName);
System.out.println(trainerEmail);
System.out.println(trainerPhone);
// I have access to Only Student Object and want trainer Email and Phone
trainerName = student3.getCourse().getTrainer().getTrainerName();
trainerEmail = student3.getCourse().getTrainer().getTrainerEmail();
trainerPhone = student3.getCourse().getTrainer().getTrainerPhone();
5) Demo2.java
package com.jlcindia.demos1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo2 {
if (mystudent != null) {
Course mycourse = mystudent.getCourse();
if (mycourse != null) {
Trainer mytrainer = mycourse.getTrainer();
if (mytrainer != null) {
trainerName = mytrainer.getTrainerName();
trainerEmail = mytrainer.getTrainerEmail();
trainerPhone = mytrainer.getTrainerPhone();
}
}
}
System.out.println(trainerName);
System.out.println(trainerEmail);
System.out.println(trainerPhone);
}
}
Files Required:
1. Trainer.java 2. Course.java
3. Student.java 4. Demo1.java
5. Demo2.java 6. Demo3.java
7. Demo4.java
1) Trainer.java
package com.jlcindia.demos3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Trainer {
private String trainerName;
private String trainerEmail;
private String trainerPhone;
public Trainer() {}
public Trainer(String trainerName, String trainerEmail, String trainerPhone) {
this.trainerName = trainerName;
this.trainerEmail = trainerEmail;
this.trainerPhone = trainerPhone;
}
//Setters and Getters
}
2) Course.java
package com.jlcindia.demos3;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Course {
private String courseName;
private double coursePrice;
private Optional<Trainer> trainer;
public Course() {}
3) Student.java
package com.jlcindia.demos3;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Student {
private String studentName;
private String studentEmail;
private long studentPhone;
private Optional<Course> course;
public Student() {
}
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
System.out.println(name);
System.out.println(email);
System.out.println(phone);
}
}
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo2 {
System.out.println(name);
System.out.println(email);
System.out.println(phone);
}
}
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
public static void main(String[] args) {
System.out.println(name);
System.out.println(email);
System.out.println(phone);
System.out.println("-------------------");
System.out.println(cname);
System.out.println(price);
}
}
7) Demo4.java
package com.jlcindia.demos3;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
Course course=null;
Student student=new Student("Hello", "hello@gmail", 111, Optional.ofNullable(course));
System.out.println("-------------------");
System.out.println(cname);
System.out.println(price);
}
}
Demo1.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println("--------------------");
System.out.println("--------------------");
System.out.println(numsList);
}
}
Demo2.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
/*
System.out.println("-------------");
Stream<Integer> mystream2 = numsList.stream(); //1
mystream2.forEach(System.out::println); //3
System.out.println("-------------");
Stream<Integer> mystream3 = numsList.stream(); //1
mystream3.forEach(System.out::println); //3
}
}
Demo3.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
public static void main(String[] args) {
numsList.stream().forEach(System.out::println);
System.out.println("---------------------");
numsList.parallelStream().forEach(System.out::println);
}
}
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
mylist.stream().forEach(System.out::println);
System.out.println("---------------------");
mylist.stream()
.map(Collection::stream)
.forEach(System.out::println);
System.out.println("---------------------");
mylist.stream()
.map(Collection::stream)
.flatMap(input -> input)
.forEach(System.out::println);
}
}
Demo5.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
mylist.stream().limit(10).forEach(System.out::println);
System.out.println("---------------------");
mylist.stream()
.limit(10)
.filter(num -> num%2!=0)
.map(num -> num * num)
.forEach(System.out::println); //3
}
}
Demo6.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo6 {
public static void main(String[] args) {
mylist.stream().skip(4).limit(9).forEach(System.out::println);
System.out.println("---------------------");
mylist.stream() //1
.skip(4)
.limit(9)
.filter(num -> num%2!=0)
.map(num -> num * num)
.forEach(System.out::println); //3
}
}
import java.util.Arrays;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo7 {
public static void main(String[] args) {
System.out.println("---------------------");
mylist.stream() //1
.skip(3)
.limit(5)
.peek(System.out::println)
.filter(num -> num%2!=0)
.peek(System.out::println)
.map(num -> num * num)
.forEach(System.out::println); //3
}
}
Demo8.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo8 {
public static void main(String[] args) {
System.out.println("---------------------");
mylist.stream()
.limit(15)
.distinct()
.filter(num -> num%2!=0)
.map(num -> num * num)
.sorted()
.forEach(System.out::println);
}
}
Demo9.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo9 {
public static void main(String[] args) {
System.out.println("-----1------");
Stream<Integer> myStream1 = Stream.empty();
myStream1.forEach(System.out::println);
System.out.println("------2------");
Stream<Integer> myStream2 = Stream.of(11,12,13);
myStream2.forEach(System.out::println);
System.out.println("-----3------");
Stream<Integer> myStream3 = numsList.stream();
myStream3.forEach(System.out::println);
Demo10.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo10 {
public static void main(String[] args) {
Demo11.java
package com.jlcindia.demos;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println("-------------------");
Stream.iterate(11, n-> n+1)
.limit(10)
.filter(num -> num %2==0)
.forEach(System.out::println);
System.out.println("-------------------");
Stream.iterate(11, n-> n+1)
.limit(10)
.filter(num -> num %2!=0)
.forEach(System.out::println);
}
}
Demo12.java
package com.jlcindia.demos;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo12 {
System.out.println("-------------------");
Stream.iterate(101, n-> n+1)
.skip(25)
.limit(10)
.filter(num -> num %2==0)
.forEach(System.out::println);
Demo13.java
package com.jlcindia.demos;
import java.util.Random;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo13 {
public static void main(String[] args) {
System.out.println("-------------------");
Stream.generate(() -> (new Random()).nextInt(100))
.limit(10)
.filter(num -> num % 2 != 0)
.map(num -> num * num)
.forEach(System.out::println);
System.out.println("-------------------");
Stream.generate(() -> (new Random()).nextInt(100))
.limit(10)
.filter(num -> num % 2 == 0)
.map(num -> num * num)
.forEach(System.out::println);
System.out.println("Done!!!");
}
}
import java.util.Arrays;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo14 {
public static void main(String[] args) {
System.out.println(lastElement);
}
}
Demo15.java
package com.jlcindia.demos;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
mylist.stream()
.filter(num -> num%2!=0)
.map(num -> num * num)
.forEach(System.out::println); //3
System.out.println("---------------------");
System.out.println("---------------------");
Optional<Integer> minNum = mylist.stream()
.filter(num -> num%2!=0)
.map(num -> num * num)
.min((num1, num2) -> num1.compareTo(num2));
System.out.println(minNum);
minNum.ifPresent(System.out::println);
System.out.println("---------------------");
System.out.println("Done!!!");
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println("-------------");
System.out.println("Done!!!");
}
}
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println(mylist1);
System.out.println("-------------");
List<Integer> mylist2 = numsList.stream()
.filter(num -> num % 2 == 0)
.map(num -> num * num)
.sorted()
.collect(Collectors.toCollection(LinkedList::new));
System.out.println(mylist2);
System.out.println("-------------");
System.out.println("Done!!!");
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
System.out.println("-------------");
Set<Integer> myset1 = numsList.stream()
.filter(num -> num % 2 == 0)
.map(num -> num * num)
.sorted()
.collect(Collectors.toSet());
System.out.println(myset1);
System.out.println("-------------");
Set<Integer> myset2 = numsList.stream()
.filter(num -> num % 2 == 0)
.map(num -> num * num)
.sorted()
.collect(Collectors.toCollection(TreeSet::new));
System.out.println(myset2);
System.out.println("-------------");
System.out.println("Done!!!");
}
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo19 {
public static void main(String[] args) {
System.out.println(count);
System.out.println(sumResult);
System.out.println(mulResult);
System.out.println(result);
}
}