Showing posts with the label Java Streams

5 Ways: Java Program to Capitalize the first character of each word in a string

In this section, we will show five different ways to  capitalize the first letter of each word in a string in Java. 1. Using For loop and toUpperCase() method 2. Using Java 8 Streams 3. Using regex and replaceAll() method 4. Using Apache Commons Text 5. Using Google Guava 1. Using For loop and  toUpperCase() method public class Main { public static void main ( String [] args) { String string = "java is awesome" ; String [] arr = string .split( " " ); StringBuilder stringBuilder = new StringBuilder (); for ( int i = 0 ; i < arr . length ; i ++) { stringBuilder .append( Character . toUpperCase ( arr [ i ].charAt( 0 ))) .append( arr [ i ].substring( 1 )).append( " " ); } String result = stringBuilder .toString(); System . out .println( result ); } } Here we are using split() split the string based on whitespace. It returns an Array of String. Iterate

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

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; 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 = .collect(Collectors.groupingBy(item -> ai .getAndIncrement() / chunkSize)) .values(); chun

Java 8 Stream - Remove duplicates from a list of objects based on a property

In this section, we will show you how to remove duplicates from a list of objects based on a single property.  In this example, we are getting the stream from the list and putting it in the TreeSet from which we provide a custom comparator that compares id, email, or salary uniquely.   Here we are going to remove duplicates based on the,  id(Long) property of the user  email(String) property of the user  salary(Double) property of the user public class User { private Long id ; private String name ; private String email ; private String phone ; private Double salary ; public Long getId () { return id ; } public void setId ( Long id) { this . id = id; } public String getName () { return name ; } public void setName ( String name) { this . name = name; } public String getEmail () { return email ; } public void setEmail ( String email) { this . email = email; }

How to transform HashMap to another HashMap using Java 8 Collectors toMap

Hello everyone, here we will show you how to convert HashMap<Integer, User> to another HashMap<Integer, UserDto> using Java 8 Collectors toMap. The  Collectors.toMap returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements. Overview of components used in this example Java Stream API The Java Stream API provides a functional approach to processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source Collection or Array. Most of the stream operations return a Stream. This helps create a chain of stream operations(stream pipe-lining). The streams also support the aggregate or terminal operations on the elements. for example, finding the minimum or maximum element or finding average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream. Collectors.toMap The