0 votes
1.8k views
in Java by
How will you print count of empty strings in java 8?
For Example:-
["abc", "", "bc", "fg", "cd","", "jkl", ""]

1 Answer

0 votes
by (2.8k points)
In java 8, you want to count an empty string then you must aware about isEmpty(), count(), stream() and filter() methods.

isEmpty():- Check string is empty or not.
count():- count the string.
stream():- Returns a sequential Stream with this collection as its source.
filter():- Returns a stream consisting of the elements of this stream that match the given predicate.

See the following running example to count of empty strings:-

package com.java8.array;

import java.util.Arrays;
import java.util.List;

public class CountStringsInArray {

    public static void main(String[] args) {
        List<String> arrStr = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        //Count empty strings
        long count = arrStr.stream().filter(x -> x.isEmpty()).count();
        System.out.println("Empty Count: " + count);
    }

}
Output:-
Empty Count: 2

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...