Posts

Showing posts with the label Learn Java 8 streams with example

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

Learn Java 8 streams with an example - count() and filter() method

Example 1: Counting Empty String import java.util. *; import java.util.stream.Collectors ; /*Counting Empty String*/ public class DriverClass { public static void main ( String [] args) { List < String > list = Arrays . asList ( "ss" , "" , "ss" , "" , "" ); long count = list .stream().filter(o -> o.isEmpty()).count(); System . out .println( count ); } } Example 2: Count number of String which ends with "d" import java.util. *; /*Count number of String which ends with "d"*/ public class DriverClass { public static void main ( String [] args) { List < String > list = Arrays . asList ( "method" , "static" , "void" , "abstract" , "ping" ); long count = list .stream().filter(x -> x.endsWith( "d" )).count(); System . out .println( count ); } }

Learn Java 8 streams with an example - Filter a Map by keys and values

Image
In this section, we will learn how to filter a Map using the Stream filter() method. Example 1: Filter Map by Keys import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; /*Filter Map by Keys*/ public class DriverClass { public static void main( String [] args) { Map < Integer , String > hMap = new HashMap < Integer , String >(); hMap.put( 123 , "Jessie" ); hMap.put( 234 , "Alpha" ); hMap.put( 345 , "Beta" ); hMap.put( 456 , "Tesla" ); hMap.put( 111 , "Tera" ); Map < Integer , String > output = hMap.entrySet() .stream() .filter(map -> map.getKey().intValue() <= 254 ) .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); System.out.println( "Output: " + output); } } Output:  Example 2: Filter Map by Va