Home java Find the number of entries Substings in the string Java Stream API

Find the number of entries Substings in the string Java Stream API

Author

Date

Category

asked a task at the interview: to find the number of entries settings in the string using Java Stream API .

Example:

string str = "Multiple Findme Classes and Nested" +
    "Static Findme Classes Are Supported," +
    "PRINTING UNWANTED OR ILL-FORMATTED DATA" +
    "To Output Will Cause The Test Findmecases" +
    "to failfindme";
STRING SUBSTR = "FINDME";

Result:

4

Answer 1, Authority 100%

Streaming can also be solved effectively (in this case, without creating additional arrays / collections and without using regular expressions), although not very clearly, it is essentially veiled for.

(The advantages of the stream will be visible if the task change and will need to do any other additional operations)

long count = intstream.iterate (
      Str.Indexof (substr), // begin with the first entry
      I - & gt; I! = -1, // until we get a negative answer
      I - & gt; Str.Indexof (substr, i + 1)) // We are looking for the following entry
    .count (); // We consider entry
System.out.PrintLN (Count);

code will work on large lines,
as well as with intersecting substrates,
For example

string str = "aaaaaaa";
STRING SUBSTR = "AA";

PS: java9 +


Answer 2, Authority 200%

Solution 1: Use the filter.

string str = "Multiple Findme Classes and Nested" +
    "Static Findme Classes Are Supported," +
    "PRINTING UNWANTED OR ILL-FORMATTED DATA" +
    "To Output Will Cause The Test Findmecases" +
    "to failfindme";
STRING SUBSTR = "FINDME";
Long Count =.
  // Separate the source string on the blank
  // symbols for an array of words and go around
  Array.stream (str.split ("\\ s"))
    // Filter Stream according to
    // containing substring
    .filter (Word - & gt; Word.contains (substr))
    // withdraw the number of words
    .count ();
System.out.PrintLN (Count); // 4

This algorithm will not work for those cases when in any word the desired substring is found twice or more.


Solution 2: should work in all cases, including for two or more entries Substings in one word, also for numbers, special and so

long count =
  // add to the source line any
  // Symbol and divide it on the substring.
  // Come goes around the array of parts of the source line
  Array.stream ((Str + ") .split (substr))
    // We consider parts
    .count ();
System.out.PrintLN (Count - 1); // 4

Solution 3: Simplified option, without streams.

int count = (str + ") .split (substr) .length;
System.out.PrintLN (Count - 1); // 4

Answer 3, Authority 100%

Public Static Void Main (String [] Args) {
  String str = "Multiple FindMe Classes and Nested" +
      "Static Findme Classes Are Supported," +
      "PRINTING UNWANTED OR ILL-FORMATTED DATA" +
      "To Output Will Cause The Test Findmecases" +
      "to failfindme";
  STRING SUBSTR = "FINDME";
  Long Count = Arrays.Stream (str.split ("))
      .Filter (S - & gt; s.contains (substr))
      .count ();
  System.out.PrintLN (Count);
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions