Posts

Showing posts from March, 2021

String operations - Learn Java 8 with examples

For more topics, please click the below link: Learn Java 8 streams with an example - How to sort a list Learn Java 8 streams with an example - Filter a Map by keys and values Learn Java 8 streams with an example - Summing Numbers Java 8 program to print odd/even numbers from array and List Learn Java 8 streams with an example - count() and filter() method Learn Java 8 streams with an example - map() method Java Lambda Expressions Java 8, Streams to find the duplicate elements String operations - Learn Java 8 with examples Example 1: Splitting Strings With Stream API Split a white space separated String into a list of String using Stream API import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class DriverClass { public static void main(String[] args) { String str = "I love my country" ; Stream. of (str.split( " " )) .map(elem -> new String(elem)) .collect(Collectors.

Java 8, Streams to find the duplicate elements

Example 1: Using Collections.frequency() It compares each item with a list – Collections.frequency(list, i). 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() Create a Map by Collectors.groupingBy and find elements that count > 1 import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Colle

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 other by applying a function. Example 1: Stream map() function with 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 a Stream of Strings to a Stream of Integers import java.util. *; import java.util.stream.Collectors ; /

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 ); } } Example 3: Count numb

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

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 , "Sibin" ); 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: {234=Alpha, 123=Sibin} Example 2: Filter Map by Values import java.util.HashMap ; import java.util.Map ; import java.util.stream.Co

Learn Java 8 streams with an example - print odd/even numbers from array and List

Example 1: Java 8 program to print odd numbers from a List import java.util.Arrays ; import java.util.List ; /*Java 8 Program to find Odd Numbers from a list*/ public class DriverClass { public static void main ( String [] args) { List < Integer > numbers = Arrays . asList ( 1 , 4 , 8 , 40 , 11 , 22 , 33 , 99 ); numbers .stream().filter(o -> o % 2 != 0 ).forEach( System . out ::println); } } Example 2: Java 8 program to print even numbers from a List import java.util.Arrays ; import java.util.List ; /*Java 8 Program to find Even Numbers from a list*/ public class DriverClass { public static void main ( String [] args) { List < Integer > numbers = Arrays . asList ( 1 , 4 , 8 , 40 , 11 , 22 , 33 , 99 ); numbers .stream().filter(o -> o % 2 = = 0 ).forEach( System . out ::println); } } Example 3: Java 8 program to print even numbers from an array import java.util.Arrays ; /*Java 8 Program to find Even Numbers from an Array*/