Posts

Showing posts with the label Learn Java 8 streams with example

Java Program to Find the First Non Repeating Word in a String

Image
In this section, we will show you two different ways to find first non repeating word in a given String in Java. 1.  Using For loop and Map 2. Using  Java 8 Streams Example 1. Using For loop and Map import java.util.LinkedHashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String str = "go python java go kotlin python kotlin" ; Map < String , Integer > map = new LinkedHashMap<>(); String [] words = str .split( " " ); for ( String word : words ) { if ( map .containsKey( word )) { map .put( word , map .get( word ) + 1 ); } else { map .put( word , 1 ); } } for ( Map . Entry < String , Integer > entry : map .entrySet()) { if ( entry .getValue()== 1 ) { System . out .println( entry .getKey()); break ; } }

Java Program to Find the First Repeated Word in a String

Image
In this section, we will show you two different ways to find first repeated word in a given String in Java. 1.  Using For loop and Map 2. Using  Java 8 Streams Example 1. Using For loop and Map import java.util.LinkedHashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String str = "kotlin java python java go python " + "go java python java go python go" ; Map < String , Integer > map = new LinkedHashMap<>(); String [] words = str .split( " " ); for ( String word : words ) { if ( map .containsKey( word )) { map .put( word , map .get( word ) + 1 ); } else { map .put( word , 1 ); } if ( map .get( word ) > 1 ) { System . out .println( word ); break ; } } } } Here the logic is simple,  split() method is used to split

Java Program to Find Duplicate Words in a String - 3 Ways

Image
In this section, we will show you three different ways to find duplicate words in a given String in Java. 1.  Using For loop and Map  2. Using Java 8 Streams 3. Using Google Guava MultiSet Example 1: Using For loop and Map  import java.util.HashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String string = "Java Python Go C# Java Python Java Go C" ; //Split the string into words based on whitespace String words [] = string .split( " " ); Map < String , Integer > occurrences = new HashMap<>(); //Iterate through String array for ( String word : words ) { Integer oldCount = occurrences .get( word ); if ( oldCount == null ) { oldCount = 0 ; } occurrences .put( word , oldCount + 1 ); } for ( Map . Entry < String , Integer > entry : occurrences .entrySet()) if ( entr

Java Program to Find Frequency of each Word in a String

Image
In this section, we will show you three different ways to find frequency of each word in a given String in Java. 1.  Using For loop and Map  2. Using Java 8 Streams 3. Using Google Guava MultiSet Example 1: Using For loop and Map  import java.util.HashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String string = "Java Python Go C# Java Python Java Go C" ; //Split the string into words based on whitespace String words [] = string .split( " " ); Map < String , Integer > occurrences = new HashMap<>(); //Iterate through String array for ( String word : words ) { Integer oldCount = occurrences .get( word ); if ( oldCount == null ) { oldCount = 0 ; } occurrences .put( word , oldCount + 1 ); } System . out .println( occurrences ); } } 1.  split() method in Java splits a string in