Automation QA Testing Course Content

Interview Programs on Streams in Java 8

Count Occurrence of each character in String using Java 8

In this tutorial we will learn How to count occurrences of a character from string using java8 stream API.

First we will split string based on “” empty string value and will store into Array of string and after that we will use Arrays.Stream(-) method and convert into stream and after converting into stream we can call .collect(-) method.Inside collect(-) method we can pass the parameter Collectors.groupingBy(-,-) and Inside Collectors.groupingBy(-,-) function we can pass the two parameters one is Function.identity() to find the unique identity of each element and second parameter we can pass Collectors.counting() to count the number of occurrence for each character.

String input = "gainjavaknowledge";

String [] array = input.split("");

Map<String, Long> output = Arrays.stream(array).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CountCharacterOccurrence {

public static void main(String[] args) {
String input = "gainjavaknowledge";

Map<String, Long> output = Arrays.stream(input.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println("Output : "+output);

}
}
=============================================================================

Java Program To Demonstrate The Differences Between Java 8 Map() And flatMap() :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
class Institute
{  
    String name;
     
    List<String> locations;
     
    public Institute(String name, List<String> locations)
    {
        this.name = name;
        this.locations = locations;
    }
     
    public String getName()
    {
        return name;
    }
     
    public List<String> getLocations()
    {
        return locations;
    }
}
 
public class Java8MapAndFlatMapExample
{
    public static void main(String[] args)
    {
        List<Institute> instituteList = new ArrayList<>();
         
        instituteList.add(new Institute("IIM", Arrays.asList("Bangalore", "Ahmedabad", "Kozhikode", "Lucknow")));
        instituteList.add(new Institute("IIT", Arrays.asList("Delhi", "Mumbai", "Kharagpur")));
        instituteList.add(new Institute("NIFT", Arrays.asList("Hyderabad", "Mumbai", "Patna", "Bangalore")));
         
        //Java 8 Map() : Get names of all institutes
         
        List<String> namesOfInstitutes = instituteList.stream().map(Institute::getName).collect(Collectors.toList());
         
        System.out.println(namesOfInstitutes);
         
        //Java 8 FlatMap() : Get unique locations of all institutes
         
        Set<String> locationsOfInstitutes = instituteList.stream().flatMap(institute -> institute.getLocations().stream()).collect(Collectors.toSet());
         
        System.out.println(locationsOfInstitutes);
    }
}








































































































No comments:

Post a Comment

Note: Only a member of this blog may post a comment.