Posts

Showing posts with the label Learn Java 8 streams with example

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

Learn Java 8 streams with an example - map() method

The map() function is a method in the Stream class that represents a functional programming concept. In simple words, the map() is utilized to transform one object into another by applying a function. Example 1: Stream map() function with the operation of converting uppercase to lowercase. import java.util. *; import java.util.stream.Collectors ; /*Stream map() function with operation of converting uppercase to lowercase. */ public class DriverClass { public static void main ( String [] args) { List < String > list = Arrays . asList ( "SAW" , "NUN" , "JOKER" , "REVENANT" , "CAST AWAY" ); List < String > result = list .stream(). map( String ::toLowerCase). collect( Collectors . toList ()); System . out .println( result ); } } Example 2: Java program to convert List of String to List of Integers import java.util. *; i