- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to get frequency of words with Lambda Expression
Let’s say the following is the List −
List<String> list = Arrays.asList("Welcome", "to","the","club", "club", "the");
No, let us create a Map to get the frequency of words. Here, we are using Lambda Expression as well −
Map<String, Integer> map = list .parallelStream() .flatMap(a -> Arrays.asList(a.split(" ")).stream()) .collect( Collectors.toConcurrentMap(c ->c.toLowerCase(), c -> 1, Integer::sum));
Example
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Demo { public static void main(String[] args) { List<String> list = Arrays.asList("Welcome", "to","the","club", "club", "the"); Map<String, Integer> map = list .parallelStream() .flatMap(a -> Arrays.asList(a.split(" ")).stream()) .collect( Collectors.toConcurrentMap(c ->c.toLowerCase(), c -> 1, Integer::sum)); System.out.println("Word Frequency = "+map); } }
Output
Word Frequency = {the=2, club=2, to=1, welcome=1}
- Related Articles
- Java Lambda Expression with Collections
- Java Program to Iterate over ArrayList using Lambda Expression
- How to implement IntFunction with lambda expression in Java?
- Java Program to pass lambda expression as a method argument
- Java Program to get the reverse of an Integer array with Lambda Expressions
- Lambda expression in Java 8
- How to implement Function interface with lambda expression in Java?\n
- How to write a conditional expression in lambda expression in Java?
- How to use this and super keywords with lambda expression in Java?
- Type Inference in Lambda expression in Java?
- How to implement IntBinaryOperator using lambda expression in Java?
- How to implement ToIntBiFunction using lambda expression in Java?
- How to implement ToDoubleBiFunction using lambda expression in Java?
- How to implement ToLongFunction using lambda expression in Java?
- How to implement ToLongBiFunction using lambda expression in Java?

Advertisements