Counting word occurence with arrays stream in Java 8

  • A+
Category:Languages

How I can calculate word frequency in a string with using arrays stream? I'm using Java 8.

Here is my code:

String sentence = "The cat has black fur and black eyes"; String[] bites = sentence.trim().split("//s+");  String in = "black cat"; 

calculate words "black" and "cat" frequency in the sentence. Word "black" frequency is 2 and word "cat" is 1.

So the goal output is then 3.


    String sentence = "The cat has black fur and black eyes";     String[] bites = sentence.trim().split("//s+");     String in = "black cat";     long i = Stream.of(bites).filter(e->(Arrays.asList(in.split("//s")).contains(e))).count();     System.out.println(i); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: