package com.vinod.test;
public class Java8ThreadExample {
public static void main(String[] args) {
Thread t1 = new Thread(Java8ThreadExample::printJob);
Thread t2 = new Thread(Java8ThreadExample::printJob);
t1.start();
t2.start();
}
public static void printJob() {
for (int i = 1; i <= 15; i++) {
System.out.println(Thread.currentThread()+"printing" + i);
}
}
}
Java 8 Method Reference example
Java 8 StringJoiner Example
Example
package com.vinod.test;
import java.util.StringJoiner;
public class Java8StringJoinerExample {
public static void main(String[] args) {
StringJoiner sj = new StringJoiner("-");
sj.add("Honda").add("Toyota").add("Ford");
System.out.println(sj);
// String joiner with prefix and suffix
StringJoiner sj1 = new StringJoiner("-", "My Vehicle List start",
"My Vehicle List end");
sj1.add("Honda").add("Toyota").add("Ford");
System.out.println(sj1);
// Merge two string joiner
System.out.println(sj.merge(sj1));
}
}
Output
Honda-Toyota-Ford
My Vehicle List startHonda-Toyota-FordMy Vehicle List end
Honda-Toyota-Ford-Honda-Toyota-Ford
Java 8 Stream aggregate operations example
Java 8 Streams
Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.
Aggregate operations − Stream supports aggregate operations like filter,map, limit, reduce, find, match, and so on.
Here is one simple example which is using aggregate operations
package com.vinod.test;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class JavaStreamExample {
public static void main(String[] args) {
Employee emp1 = new Employee("Vinod", "Admin", "CA", 33);
Employee emp2 = new Employee("Santhosh", "SD", "CA", 34);
Employee emp3 = new Employee("Anish", "Fin", "CA", 30);
Employee emp4 = new Employee("Raghav", "Sales", "CA", 12);
Employee emp5 = new Employee("Raghav", "Sales", "CA", 12);
List<Employee> empList = new ArrayList<Employee>();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
empList.add(emp4);
empList.add(emp5);
System.out.println("Before java 8");
for (Employee emp : empList) {
System.out.println(emp);
}
// java 8
System.out.println("Java 8 Iterating List");
empList.stream().forEach(System.out::println);
// Filter
System.out.println("Java 8 filtering a list");
List<Employee> filterEmployee = empList.stream()
.filter(e -> e.getName().equalsIgnoreCase("Vinod"))
.collect(Collectors.toList());;
System.out.println(filterEmployee.toString());
// Limit
List<Employee> limitEmployee = empList.stream().limit(2)
.collect(Collectors.toList());;
System.out.println(limitEmployee);
//Match
boolean matchEmployee = empList.stream().anyMatch(e->e.getAge()<30);
System.out.println("Match :"+matchEmployee);
//Map example
IntSummaryStatistics stats = empList.stream()
.mapToInt((x) -> x.getAge()).summaryStatistics();
System.out.println("Highest age in List : " + stats.getMax());
System.out.println("Lowest age in List : " + stats.getMin());
System.out.println("Average of all ages : " + stats.getAverage());
}
}
Output
Before java 8
Employee [name=Vinod, department=Admin, address=CA, age=33]
Employee [name=Santhosh, department=SD, address=CA, age=34]
Employee [name=Anish, department=Fin, address=CA, age=30]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Java 8 Iterating List
Employee [name=Vinod, department=Admin, address=CA, age=33]
Employee [name=Santhosh, department=SD, address=CA, age=34]
Employee [name=Anish, department=Fin, address=CA, age=30]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Java 8 filtering a list
[Employee [name=Vinod, department=Admin, address=CA, age=33]]
[Employee [name=Vinod, department=Admin, address=CA, age=33], Employee [name=Santhosh, department=SD, address=CA, age=34]]
Match :true
Highest age in List : 34
Lowest age in List : 12
Average of all ages : 24.2
Java 8 Default Method Example
Java 8 Default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface.
Here is one example, the Civic and Accord class implementing the same interface and one only Accord is the only class implementing the default method.
package com.vinod.test;
public interface Vehicle {
void printName(String name);
default public void printColor(String color) {
System.out.println(color);
}
}
package com.vinod.test;
public class Civic implements Vehicle{
@Override
public void printName(String name) {
System.out.println("I am Vehicle ...." + name);
}
}
package com.vinod.test;
public class Accord implements Vehicle {
@Override
public void printName(String name) {
System.out.println("I am Vehicle ...." + name);
}
@Override
public void printColor(String color) {
System.out.println("My color is ...." + color);
}
}
package com.vinod.test;Output
public class Java8DefaultMethodExample {
public static void main(String[] args) {
Vehicle civic = new Civic();
Vehicle accord = new Accord();
civic.printName("Civic");
accord.printName("Accord");
accord.printColor("White");
}
}
I am Vehicle ....Civic
I am Vehicle ....Accord
My color is ....White
Java 8 Executing Runnable using Lambda expression
In java 8 it is very easy to execute a runnable using Lambda.. see how it works package com.vinod.test;
package com.vinod.test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Java8RunnableExample {
private static ExecutorService executor = null;
public static void main(String[] args) {
Runnable r = () -> print();
executor = Executors.newFixedThreadPool(2);
executor.submit(r);
}
private static void print() {
System.out.println("Vinod");
}
}
Output
Vinod
Java 8 Comparator Example
1. Create a java class
package com.vinod.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Java8LambdaComparatorExample {
public static void main(String[] args) {
Employee emp1 = new Employee("Vinod", "Admin", "CA", 33);
Employee emp2 = new Employee("Santhosh", "SD", "CA", 34);
Employee emp3 = new Employee("Anish", "Fin", "CA", 30);
Employee emp4 = new Employee("Raghav", "Sales", "CA", 12);
Employee emp5 = new Employee("Raghav", "Sales", "CA", 12);
List empList = new ArrayList();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
empList.add(emp4);
empList.add(emp5);
List empNewList = new ArrayList();
empNewList.addAll(empList);
// Java 7
System.out.println("Java 7 Sorting using comparator");
Collections.sort(empList, new Comparator() {
public int compare(Employee emp1, Employee emp2) {
return emp1.getName().compareTo(emp2.getName());
}
});
for (Employee e : empList) {
System.out.println(e);
}
// Java 8
System.out.println("Java 8 Sorting using Lambda expression");
Collections.sort(empNewList,
(s1, s2) -> s1.getName().compareTo(s2.getName()));
System.out.println("after sort");
empNewList.stream().forEach(System.out::println);
}
}
2. Output
Java 7 Sorting using comparator
Employee [name=Anish, department=Fin, address=CA, age=30]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Employee [name=Santhosh, department=SD, address=CA, age=34]
Employee [name=Vinod, department=Admin, address=CA, age=33]
Java 8 Sorting using Lambda expression
after sort
Employee [name=Anish, department=Fin, address=CA, age=30]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Employee [name=Raghav, department=Sales, address=CA, age=12]
Employee [name=Santhosh, department=SD, address=CA, age=34]
Employee [name=Vinod, department=Admin, address=CA, age=33]
Confusion Matrix + Precision/Recall (Super Simple, With Examples)
Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...
Featured Posts
-
In this example we will see how do to the file encryption and decryption using Apache Camel using pgp 1. Generate pgp keys In order to do t...
-
Spring provides a JMS integration framework that simplifies the use of the JMS API, the JmsTemplate class is the core class which is availab...
-
The selective consumer is consumer that applies a filter to incoming messages so that only messages meeting that specific selection criteria...
-
Apache camel API has the inbuilt kafka component and it is very simple to create producer, consumer and process messages. Here is one simple...
-
Apache camel provides intercepting feature while Exchanges are on route. Camel supports three types for interceptors ( See more about Camel ...
-
Maven Error Notes [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on projec...
-
In the previous example ( Mocking Static methods ) we created mock values for Static methods, in this example we will see how to mock new in...
-
🔢 Java Sorting Algorithms — Step-by-Step with Iterations Sorting is a fundamental concept in programming and data structures. Below are t...
-
Itext PDF is an open source API that allows to create and modify pdf documents in java. In this example we will see how to create and and i...
-
Camel Timer component is used to generate or process message exchanges when a time fires. Here is one example to print ‘Hello world ‘ in eac...