Posts

Showing posts with the label Learn Java 8 streams with example

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

Java 8 Streams - Sum of all the items of the List

Example 1: Using  Collectors.summingInt() import java.util.Arrays ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < Integer > integers = Arrays . asList ( 2 , 9 , 10 , 8 , 4 , 7 ); Integer sum = integers .stream() .collect( Collectors . summingInt ( Integer ::intValue)); System . out .println( "Sum= " + sum ); } } Here, stream() method returns a sequential Stream with List as its source. summingInt() method returns a Collector that produces the sum of a integer.   Similarly, the Collectors class provides summingDouble() and summingLong().   Example 2: Using Stream.reduce()   import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < Integer > integers = Arrays . asList ( 2 , 9 , 10 , 8 , 4 , 7 ); Integer sum = integers .stream() .reduce(

Learn Java 8 streams with an example - How to sort a list

  Example 1: Sort the List In the natural order import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class DriverClass { public static void main(String[] args) { List<String> list = Arrays. asList ( "H" , "A" , "Z" , "L" , "B" , "Y" , "M" , "a" , "c" ); list.stream(). sorted() .collect(Collectors. toList ()).forEach (System. out ::println); } } Output: A B H L M Y Z a c Example 2: Sort the List In the reverse Order import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class DriverClass { public static void main(String[] args) { List<String> list = Arrays. asList ( "H" , "A" , "Z" , "L" , "B" , "Y" , "M" , "a" , "c" ); li