-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
742 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ Java™ Platform Standard Ed. 8 | |
:numbered: 0 | ||
|
||
jdkJre | ||
jdk8 | ||
Lambda | ||
javap | ||
instrumentation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# Java 8 Tutorial | ||
|
||
## Overview | ||
### New Features | ||
- **Lambda expression** − Adds functional processing capability to Java. | ||
- **Method references** − Referencing functions by their names instead of invoking them directly. Using functions as parameter. | ||
- **Default method** − Interface to have default method implementation. | ||
- **New tools** − New compiler tools and utilities are added like ‘jdeps’ to figure out dependencies. | ||
- **Stream API** − New stream API to facilitate pipeline processing. | ||
- **Date Time API** − Improved date time API. | ||
- **Optional** − Emphasis on best practices to handle null values properly. | ||
- **Nashorn, JavaScript Engine** − A Java-based engine to execute JavaScript code. | ||
|
||
## Lambda Expressions | ||
### Syntax | ||
``` | ||
parameter -> expression body | ||
``` | ||
|
||
### Lambda Expressions Example | ||
``` | ||
MathOperation addition = (int a, int b) -> a + b; | ||
GreetingService greetService1 = message -> System.out.println("Hello " + message); | ||
``` | ||
- `LambdaSort` | ||
- `LambdaMathService` | ||
|
||
## Method References | ||
``` | ||
List names = new ArrayList(); | ||
names.forEach(System.out::println); | ||
``` | ||
- `MethodReferences` | ||
|
||
## Functional Interfaces | ||
- `FunctionalInterfaces` | ||
|
||
## Default Methods | ||
### Syntax | ||
``` | ||
public interface vehicle { | ||
default void print() { | ||
System.out.println("I am a vehicle!"); | ||
} | ||
} | ||
``` | ||
|
||
### Multiple Defaults | ||
``` | ||
public interface fourWheeler { | ||
default void print() { | ||
System.out.println("I am a four wheeler!"); | ||
} | ||
} | ||
``` | ||
``` | ||
public class car implements vehicle, fourWheeler { | ||
public void print() { | ||
System.out.println("I am a four wheeler car vehicle!"); | ||
} | ||
} | ||
``` | ||
|
||
### Static Default Methods | ||
``` | ||
public interface vehicle { | ||
default void print() { | ||
System.out.println("I am a vehicle!"); | ||
} | ||
static void blowHorn() { | ||
System.out.println("Blowing horn!!!"); | ||
} | ||
} | ||
``` | ||
- `DefaultMethods` | ||
|
||
## Streams | ||
### Generating Streams | ||
``` | ||
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); | ||
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); | ||
``` | ||
|
||
### forEach | ||
``` | ||
Random random = new Random(); | ||
random.ints().limit(10).forEach(System.out::println); | ||
``` | ||
|
||
### map | ||
``` | ||
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); | ||
//get list of unique squares | ||
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList()); | ||
``` | ||
|
||
### filter | ||
``` | ||
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); | ||
//get count of empty string | ||
int count = strings.stream().filter(string -> string.isEmpty()).count(); | ||
``` | ||
|
||
### limit | ||
``` | ||
Random random = new Random(); | ||
random.ints().limit(10).forEach(System.out::println); | ||
``` | ||
|
||
### sorted | ||
``` | ||
Random random = new Random(); | ||
random.ints().limit(10).sorted().forEach(System.out::println); | ||
``` | ||
|
||
### Parallel Processing | ||
``` | ||
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); | ||
//get count of empty string | ||
long count = strings.parallelStream().filter(string -> string.isEmpty()).count(); | ||
``` | ||
|
||
### Collectors | ||
``` | ||
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); | ||
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); | ||
System.out.println("Filtered List: " + filtered); | ||
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", ")); | ||
System.out.println("Merged String: " + mergedString); | ||
``` | ||
|
||
### Statistics | ||
``` | ||
List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); | ||
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics(); | ||
System.out.println("Highest number in List : " + stats.getMax()); | ||
System.out.println("Lowest number in List : " + stats.getMin()); | ||
System.out.println("Sum of all numbers : " + stats.getSum()); | ||
System.out.println("Average of all numbers : " + stats.getAverage()); | ||
``` | ||
- `Streams` | ||
|
||
## Optional Class | ||
### Class Declaration | ||
``` | ||
public final class Optional<T> extends Object | ||
``` | ||
- `OptionalClass` | ||
|
||
## Nashorn JavaScript | ||
- `NashornJavaScript` | ||
|
||
## New Date/Time API | ||
- `LocalDateTimeAPI` | ||
- `ZonedDateTimeAPI` | ||
- `ChronoUnitsEnum` | ||
- `PeriodAndDuration` | ||
- `TemporalAdjusters` | ||
- `BackwardCompatibility` | ||
|
||
## Base64 | ||
- `Base64Example` | ||
|
||
## References | ||
- [Java 8 - Overview](https://www.tutorialspoint.com/java8/java8_overview.htm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package t5750.jdk8; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.util.Date; | ||
|
||
public class BackwardCompatibility { | ||
public static void main(String args[]) { | ||
BackwardCompatibility java8tester = new BackwardCompatibility(); | ||
java8tester.testBackwardCompatability(); | ||
} | ||
|
||
public void testBackwardCompatability() { | ||
//Get the current date | ||
Date currentDate = new Date(); | ||
System.out.println("Current date: " + currentDate); | ||
//Get the instant of current date in terms of milliseconds | ||
Instant now = currentDate.toInstant(); | ||
ZoneId currentZone = ZoneId.systemDefault(); | ||
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, currentZone); | ||
System.out.println("Local date: " + localDateTime); | ||
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, currentZone); | ||
System.out.println("Zoned date: " + zonedDateTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package t5750.jdk8; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.Base64; | ||
import java.util.UUID; | ||
|
||
public class Base64Example { | ||
public static void main(String args[]) { | ||
try { | ||
// Encode using basic encoder | ||
String base64encodedString = Base64.getEncoder().encodeToString( | ||
"TutorialsPoint?java8".getBytes("utf-8")); | ||
System.out.println("Base64 Encoded String (Basic) :" + base64encodedString); | ||
// Decode | ||
byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString); | ||
System.out.println("Original String: " + new String(base64decodedBytes, "utf-8")); | ||
base64encodedString = Base64.getUrlEncoder().encodeToString( | ||
"TutorialsPoint?java8".getBytes("utf-8")); | ||
System.out.println("Base64 Encoded String (URL) :" + base64encodedString); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (int i = 0; i < 10; ++i) { | ||
stringBuilder.append(UUID.randomUUID().toString()); | ||
} | ||
byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8"); | ||
String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes); | ||
System.out.println("Base64 Encoded String (MIME) :" + mimeEncodedString); | ||
|
||
} catch (UnsupportedEncodingException e) { | ||
System.out.println("Error :" + e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package t5750.jdk8; | ||
|
||
import java.time.LocalDate; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
public class ChronoUnitsEnum { | ||
public static void main(String args[]) { | ||
ChronoUnitsEnum java8tester = new ChronoUnitsEnum(); | ||
java8tester.testChromoUnits(); | ||
} | ||
|
||
public void testChromoUnits() { | ||
//Get the current date | ||
LocalDate today = LocalDate.now(); | ||
System.out.println("Current date: " + today); | ||
//add 1 week to the current date | ||
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS); | ||
System.out.println("Next week: " + nextWeek); | ||
//add 1 month to the current date | ||
LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS); | ||
System.out.println("Next month: " + nextMonth); | ||
//add 1 year to the current date | ||
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS); | ||
System.out.println("Next year: " + nextYear); | ||
//add 10 years to the current date | ||
LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES); | ||
System.out.println("Date after ten year: " + nextDecade); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package t5750.jdk8; | ||
|
||
public class DefaultMethods { | ||
public static void main(String args[]) { | ||
Vehicle vehicle = new Car(); | ||
vehicle.print(); | ||
} | ||
} | ||
|
||
interface Vehicle { | ||
default void print() { | ||
System.out.println("I am a vehicle!"); | ||
} | ||
|
||
static void blowHorn() { | ||
System.out.println("Blowing horn!!!"); | ||
} | ||
} | ||
|
||
interface FourWheeler { | ||
default void print() { | ||
System.out.println("I am a four wheeler!"); | ||
} | ||
} | ||
|
||
class Car implements Vehicle, FourWheeler { | ||
public void print() { | ||
Vehicle.super.print(); | ||
FourWheeler.super.print(); | ||
Vehicle.blowHorn(); | ||
System.out.println("I am a car!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package t5750.jdk8; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
public class FunctionalInterfaces { | ||
public static void main(String args[]) { | ||
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); | ||
// Predicate<Integer> predicate = n -> true | ||
// n is passed as parameter to test method of Predicate interface | ||
// test method will always return true no matter what value n has. | ||
System.out.println("Print all numbers:"); | ||
//pass n as parameter | ||
eval(list, n -> true); | ||
// Predicate<Integer> predicate1 = n -> n%2 == 0 | ||
// n is passed as parameter to test method of Predicate interface | ||
// test method will return true if n%2 comes to be zero | ||
System.out.println("Print even numbers:"); | ||
eval(list, n -> n % 2 == 0); | ||
// Predicate<Integer> predicate2 = n -> n > 3 | ||
// n is passed as parameter to test method of Predicate interface | ||
// test method will return true if n is greater than 3. | ||
System.out.println("Print numbers greater than 3:"); | ||
eval(list, n -> n > 3); | ||
} | ||
|
||
public static void eval(List<Integer> list, Predicate<Integer> predicate) { | ||
for (Integer n : list) { | ||
if (predicate.test(n)) { | ||
System.out.println(n + " "); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package t5750.jdk8; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.Month; | ||
|
||
public class LocalDateTimeAPI { | ||
public static void main(String args[]) { | ||
LocalDateTimeAPI java8tester = new LocalDateTimeAPI(); | ||
java8tester.testLocalDateTime(); | ||
} | ||
|
||
public void testLocalDateTime() { | ||
// Get the current date and time | ||
LocalDateTime currentTime = LocalDateTime.now(); | ||
System.out.println("Current DateTime: " + currentTime); | ||
LocalDate date1 = currentTime.toLocalDate(); | ||
System.out.println("date1: " + date1); | ||
Month month = currentTime.getMonth(); | ||
int day = currentTime.getDayOfMonth(); | ||
int seconds = currentTime.getSecond(); | ||
System.out.println("Month: " + month + "day: " + day + "seconds: " + seconds); | ||
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012); | ||
System.out.println("date2: " + date2); | ||
//12 december 2014 | ||
LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12); | ||
System.out.println("date3: " + date3); | ||
//22 hour 15 minutes | ||
LocalTime date4 = LocalTime.of(22, 15); | ||
System.out.println("date4: " + date4); | ||
//parse a string | ||
LocalTime date5 = LocalTime.parse("20:15:30"); | ||
System.out.println("date5: " + date5); | ||
} | ||
} |
Oops, something went wrong.