@@ -4,7 +4,184 @@ This document describe complete project with new concept of java8.
442-Lambda Expression(->) <br >
553-new Operator(::) <br >
664-Java8 API--Stream & date & functinalInterface <br >
7+ 5-Optional class <br >
8+ 6-forEach
79
8- Interface New feature
9- -----------------------
10- Added following.
10+
11+ Interface New feature (default & static method)
12+ ----------------------------------------------
13+ both above method can use to extends the interface function without effecting of implement class.
14+
15+ 1-method definition allowed with default keyword
16+ -----------------------------------------------
17+ its same as instance method of class we use default keyword as modifier of method.
18+ Example:
19+
20+ default String helloDefaultInInterface() {
21+ return "default";
22+ }
23+
24+ 2-method definition allowed with static keyword
25+ -----------------------------------------
26+ Example:
27+
28+ static String helloStaticMethodInInterface() {
29+ return "staticMethodInInterface";
30+ }
31+
32+
33+ Note: before to java-8 we used only public,abstract,strictfp keywords.
34+ now after java-8 two keywords added to used --default, static
35+ if you used any others keyword you will get following error.
36+ "Illegal modifier for the interface method helloStaticMethodInInterface; only public, abstract, default, static and strictfp are permitted"
37+
38+ strictfp
39+ ------------
40+ strictfp keywords can use as method modifier or class or interface. only abstract method cant use this.
41+ it used to calculate same floating point result irrespective to platform processors.(16bit/32bit/64bit )
42+ strictfp ---> strict to floating point as result irrespective to machine processors(16bit/32bit/64bit )
43+
44+
45+
46+
47+
48+
49+ Functional Interface
50+ ----------------------
51+ The interface which have only one(not zero or more than 1) abstract method is called functional interface.
52+ we can use class level annotation to specify interface as functional.
53+
54+ example:
55+
56+ @FunctionalInterface
57+ public interface Greeting {
58+ void greet();
59+ }
60+
61+ basically functional interface normally used in lamdaexpression(functional programming).
62+
63+ LamdaExpression:
64+ ----------------
65+ lambda expression is syntactic sugar or shortest way of implementation of functional interface.
66+ in place of lambda expression we can use anonymous classes.
67+
68+ How to write LamdaExpression
69+ ----------------------------
70+ before learning Lamda we have to learn method reference operator (::) double colon.
71+
72+ There are four way to use method reference(::)
73+ 1) static method using class ....Math::max ---equivalent to Math.max(x,y)
74+ 2) instance method using object ....System.out::println ---equivalent to System.out.println(x)
75+ 3) instance method using class ....String.length ----equivalent to str.length()
76+ 4) constructor using new .... ArrayList::new ----equivalent to new ArrayList()
77+
78+ Note: argument or object value like x,y,str will be provided by Lamda-expression api automatically.
79+
80+
81+ General Syntax:
82+ --------------
83+ FunctionalInterface ref = (arugement)-> {body}
84+ (argument)-> {body} is called lamda expression--its way to provide implementation of FunctionalInterface.
85+ if method having one only argument then it cal also write in following form
86+ argument -> {body}
87+ Method reference statement also known as one type of lamda expression.
88+ FunctionalInterface ref = Method reference statement
89+
90+
91+ Example:
92+ --------
93+ 1)using method body
94+ Greeting greet1 = ()->System.out.println("LambdaExpression");
95+
96+ 2) using method reference statement here findMyAge is static method of Greeting Interface
97+ Greeting greet2 = Greeting::findMyAge
98+
99+
100+ Stream Api
101+ --------------
102+ Now java collection can used as stream of object using stream api to improve performance.
103+ Need to learn
104+ 1) foreach method
105+ 2) Stream object
106+ 3) built-in functional interface. (Function,Predicate)
107+ 4)Convert Regex to Predicate
108+
109+
110+ //using collections
111+ List<String> names = Arrays.asList("amir","meer","java","c++");
112+ names.forEach(System.out::println);
113+ Consumer<String> cons = name-> System.out.println(name.toUpperCase());
114+ names.forEach(cons);
115+
116+ //ways to create streams...
117+
118+ //1) using stream() method of collection. stream is default method of Collection Interface
119+ Stream<String> nameStreams = names.stream();
120+ nameStreams.forEach(cons);
121+
122+
123+ ///2) using Stream.of methods
124+ Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
125+ stream.forEach(System.out::println);
126+
127+ //Predicate Interface
128+ //its used to send test/validate someinput...
129+
130+ Predicate<String> pre = name -> {return name.contains("A");};
131+ System.out.println(pre.test("Amir"));
132+ System.out.println(pre.test("amir"));
133+
134+
135+ //convert RegEx to Predicate
136+ Predicate<String> emailFilter = Pattern.compile("^(.+)@example.com$").asPredicate();
137+ System.out.println("regex-"+ emailFilter.test("a@example.com"));
138+
139+
140+ //Function Interface
141+ //it use to process some input and return some output based on apply logic.
142+ Function<String,String> worker = (input) -> {return input.toLowerCase();};
143+ System.out.println(worker.apply("Amir Rizvi"));
144+
145+ //Optional class....
146+ //its solution for NullPointerException
147+
148+ String name = "Amir";
149+ String name2 = null;
150+ if (Optional.ofNullable(name).isPresent()) {
151+ System.out.println("Present-"+name);
152+ }
153+
154+ if (Optional.ofNullable(name2).isPresent()) {
155+ System.out.println("Not-Present-"+name2);
156+ }
157+
158+
159+
160+
161+
162+ Joda Date api
163+ ------------
164+ // There 4 classes for date conversion (String to Date object)
165+ //1) LocalDate.parse() 2) LocalDateTime.parse() 3)DateTimeFormatter.ofPattern(),LocalTime
166+
167+ //default date format for joda api is known as -ISO8601 date pattern
168+ //default date format for joda api is - "2019-08-07";
169+ //default DateTime format for joda api is "2016-04-04T11:50"
170+
171+ String dateOnly = "2019-08-06";
172+ LocalDate date = LocalDate.parse(dateOnly);
173+ System.out.println(date.isBefore(LocalDate.now()));
174+
175+ System.out.println(LocalDate.now()); //2019-08-07
176+ System.out.println(LocalTime.now()); //21:37:11.611
177+ System.out.println(LocalDateTime.now()); //2019-08-07T21:37:11.612
178+
179+
180+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyddMM");
181+ LocalDate parseDate = LocalDate.parse("20191011", formatter);
182+ System.out.println(parseDate); //2019-11-10
183+
184+
185+
186+
187+
0 commit comments