Posts

Showing posts with the label Java 8

Java 8 Streams - Count Frequency Of Words In a List

Image
Example 1 import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Main { public static void main( String [] args) { List < String > words = Arrays .asList( "cat" , "rat" , "bat" , "cow" , "cat" , "bat" ); // For Long values Map < String , Long > result = words.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println(result); } } The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. The asList () method of java.util.Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridg

Java 8 Streams - Program To Break A List Into Batches Of Given Size

Image
In this post, we will show how to break a list into batches of a given size. Example 1: Convert List of Characters into batches of a given size import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; public class Main { public static void main( String [] args) { List < Character > list = Arrays .asList( 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' ); int chunkSize = 3 ; AtomicInteger ai = new AtomicInteger(); Collection < List < Character >> chunkedOrders = list.stream() .collect(Collectors.groupingBy(item -> ai .getAndIncrement() / chunkSize)) .values(); chun

Java Program To Convert String To HashMap

Example 1: Convert  below String String str = "{name = rahul,email = rahul@knf.co.in,gender = male}" ; to HashMap Solution 1:   public static void main( String [] args) { String str = "{name = rahul,email = rahul@knf.co.in,gender = male}" ; //Remove curly brackets. str = str.substring( 1 , str.length() - 1 ); //Split the string by , to get key-value pairs String [] keyValuePairs = str.split( "," ); Map < String , String > map = new HashMap <>(); //Iterate over the pairs for ( String pair : keyValuePairs) { //Split the pairs to get key and value String [] entry = pair.split( "=" ); //Add them to the hashmap and trim whitespaces map.put(entry[ 0 ].trim(), entry[ 1 ].trim()); } System.out.println(map); } Solution 2: Using Java 8 Stream public static void main( String [] a

Learn Java Predicate, Consumer, Function, and Supplier (Functional Interfaces) with Example

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 write smaller and cleaner code by removing much boilerplate code.  A functional interface contains only a single abstract method and  can include default and static methods that do have an implementation and a single unimplemented method. Here we will discuss Predicate, Consumer, Function, and Supplier. 1. Predicate Interface The Predicate is a functional interface defined in java. util. Function package , which accepts an argument and returns a boolean. This is mainly used to filter data from a Java Stream. The filter method of a stream accepts a predicate to filter the data and retur