Java 8 StringJoiner Example

Java 8 introduced java.util.StringJoiner , it is a util method to construct a string with desired delimiter. Also we can add prefix and suffix to the final string. To achieve this, StringJoiner has two constructor, first is only with delimiter and second has delimiter, prefix and suffix. Here is one simple example which is using StringJoiner.

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;

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");

}

}


Output
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 = () -&gt; print();
executor = Executors.newFixedThreadPool(2);
executor.submit(r);
}

private static void print() {
System.out.println("Vinod");

}
}

Output

Vinod

 

 

 

Java 8 Comparator Example

Here is one simple example which is using sorting list of objects using Java 8 Comparator using Lambda expression.

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) -&gt; 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]

 

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts