Posts

Showing posts with the label Learn Java 8 streams with example

How to convert Set of objects to another Set of objects using Java streams?

Image
Hello everyone, here we will show you how to convert a Set to another Set in Java using Java streams map(). The ‘map’ method maps each element to its corresponding result.  Java Stream API The Java Stream API provides a functional approach to processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source Collection or Array. Most of the stream operations return a Stream. This helps create a chain of stream operations( stream pipe-lining ).  The streams also support the aggregate or terminal operations on the elements. for example, finding the minimum or maximum element or finding average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream. Stream map() Method The  Java 8 Stream map()  is an intermediate operation.It converts Stream<obj1> to Stream<obj2>. For each object of type  obj1 , a new object of type  obj2  is created and put in the new Stream.

Implement functional interfaces using lambda expressions

Image
Java is Object Oriented Programming language , being object-oriented is not bad, but it brings a lot of verbosity to the program. Java 8 introduced new libraries and programming styles, for example,  functional interfaces ,  lambda expressions , and streams . These bring functional-style programming to the object-oriented programming capabilities of Java. Java Functional Interface and Lambda Expression help us in writing smaller and cleaner code by removing a lot of boilerplate code. Java Functional Interface: A functional interface in Java is an interface that contains only a single abstract method.  A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method. Here is a Java functional interface examples: Example 1, The below counts as a functional interface in Java because it only contains a single method, and that method has no implementation: public interface Addition { public Integer calcula

String operations - Learn Java 8 with examples

Split a String import java.util.Arrays ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { String str = "I love my country" ; Arrays . stream ( str .split( " " )) .collect( Collectors . toList ()) .forEach( System . out ::println); } } Declare String which we are going to split. The stream here is created from an existing array using the Arrays.stream() method . All array elements are converted to stream elements. Here, split() method converts String based on whitespace and returns array of String. You need to pass a special object to the method collect(). This object reads all the data from the stream, converts it to a specific collection, and returns it.  toList() method Returns a Collector that accumulates the input elements into a new List. Finally, Iterate over List and print each elements to the console. Console Output:  I love my country More examples,

Java 8 Streams - How to find the duplicate elements from a Collection - Multiple ways

Example 1: Using Collections.frequency() To find frequency of a particular element we can use the frequency() method: import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class DriverClass { public static void main( String [] args) { List < String > obj = Arrays.asList( "ant" , "1" , "cat" , "ant" , "we" , "java" , "cat" ); obj.stream().filter(i -> Collections. frequency(obj, i) > 1 ).collect(Collectors.toSet()) .forEach(System.out :: println); } } Output: ant cat Example 2: Using Collectors.groupingBy() We can also use the Collectors.groupingBy() method to count the frequency of elements present in a stream.  import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import java