Java 8 Stream, get string length due to several conditionsDoes a finally block always get executed in Java?How do I read / convert an InputStream into a String in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?Get current stack trace in JavaHow to split a string in JavaConverting 'ArrayList<String> to 'String[]' in JavaGetting the Current Working Directory in JavaHow do I convert a String to an int in Java?How to Convert a Java 8 Stream to an Array?
Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?
Is it idiomatic to construct against `this`
Phrase for the opposite of "foolproof"
How to pronounce 'c++' in Spanish
Is there any official lore on the Far Realm?
On The Origin of Dissonant Chords
How could Tony Stark make this in Endgame?
Pre-plastic human skin alternative
What happens to Mjolnir (Thor's hammer) at the end of Endgame?
How does Captain America channel this power?
How would 10 generations of living underground change the human body?
Re-entry to Germany after vacation using blue card
Two field separators (colon and space) in awk
Why do games have consumables?
A Note on N!
Apply MapThread to all but one variable
Checks user level and limit the data before saving it to mongoDB
Why does Mind Blank stop the Feeblemind spell?
Minor Revision with suggestion of an alternative proof by reviewer
"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?
What is causing the white spot to appear in some of my pictures
555 timer FM transmitter
What is the most expensive material in the world that could be used to create Pun-Pun's lute?
Do I have an "anti-research" personality?
Java 8 Stream, get string length due to several conditions
Does a finally block always get executed in Java?How do I read / convert an InputStream into a String in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?Get current stack trace in JavaHow to split a string in JavaConverting 'ArrayList<String> to 'String[]' in JavaGetting the Current Working Directory in JavaHow do I convert a String to an int in Java?How to Convert a Java 8 Stream to an Array?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a program that calculate the occurrences of d
, for example s = "dda"
and n = 10
I will repeat those until I get s.length = 10
e.g ddaddaddad
the result = 7d
.
I have done this in basics loop:
int count = 0;
String s = "dda";
int n = 10;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'd')
count++;
for (int i = 0; i < n % s.length(); i++)
if (s.charAt(i) == 'd')
count++;
return count * (n / s.length());
Thus I'm trying to do that using streams, and I'm wondering how I can do it?
What I already achieved:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) + (n % s.length());
I know The problem in that last part (n % s.length())
I need to check if the index contain d
or not, but I don't know how to do that.
java loops java-stream
add a comment |
I have a program that calculate the occurrences of d
, for example s = "dda"
and n = 10
I will repeat those until I get s.length = 10
e.g ddaddaddad
the result = 7d
.
I have done this in basics loop:
int count = 0;
String s = "dda";
int n = 10;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'd')
count++;
for (int i = 0; i < n % s.length(); i++)
if (s.charAt(i) == 'd')
count++;
return count * (n / s.length());
Thus I'm trying to do that using streams, and I'm wondering how I can do it?
What I already achieved:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) + (n % s.length());
I know The problem in that last part (n % s.length())
I need to check if the index contain d
or not, but I don't know how to do that.
java loops java-stream
add a comment |
I have a program that calculate the occurrences of d
, for example s = "dda"
and n = 10
I will repeat those until I get s.length = 10
e.g ddaddaddad
the result = 7d
.
I have done this in basics loop:
int count = 0;
String s = "dda";
int n = 10;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'd')
count++;
for (int i = 0; i < n % s.length(); i++)
if (s.charAt(i) == 'd')
count++;
return count * (n / s.length());
Thus I'm trying to do that using streams, and I'm wondering how I can do it?
What I already achieved:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) + (n % s.length());
I know The problem in that last part (n % s.length())
I need to check if the index contain d
or not, but I don't know how to do that.
java loops java-stream
I have a program that calculate the occurrences of d
, for example s = "dda"
and n = 10
I will repeat those until I get s.length = 10
e.g ddaddaddad
the result = 7d
.
I have done this in basics loop:
int count = 0;
String s = "dda";
int n = 10;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'd')
count++;
for (int i = 0; i < n % s.length(); i++)
if (s.charAt(i) == 'd')
count++;
return count * (n / s.length());
Thus I'm trying to do that using streams, and I'm wondering how I can do it?
What I already achieved:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) + (n % s.length());
I know The problem in that last part (n % s.length())
I need to check if the index contain d
or not, but I don't know how to do that.
java loops java-stream
java loops java-stream
edited Mar 9 at 9:17
Ibrahim Ali
asked Mar 9 at 9:09
Ibrahim AliIbrahim Ali
13812
13812
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
all you need to add to your calculation is to substring s by the reminder and repeat the count: -
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
s.substring(0, n % s.length()).chars().filter(x -> x == 'd').count();
EDIT: if, for some reason, you dislike the oldfashined substring, you could replace it with stream of ints from 0 to the reminder:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
IntStream.range(0, n % s.length()).filter(i -> s.charAt(i)== 'd').count();
However, the question reminas whether this all-stream version is more comprehensive/readable.
I'm was thinking that I can do that without substring, e.g some methods in stream, anyway thanks.
– Ibrahim Ali
Mar 9 at 9:52
@IbrahimAli, see my edited answer
– Sharon Ben Asher
Mar 9 at 10:08
add a comment |
You might just be looking for a simpler logic if I get your question right. It could be as :
private int characterCountWithRecurrenceWithinLimit(String string, int limit, char c)
// repeat string unless its shorter than the limit
StringBuilder sBuilder = new StringBuilder(string);
while (sBuilder.length() < limit)
sBuilder.append(string);
// keep the string within the limit
String repeatedString = sBuilder.toString().substring(0, limit);
// count the character occurrence
return (int) IntStream.range(0, repeatedString.length())
.filter(i -> repeatedString.charAt(i) == c)
.count();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075732%2fjava-8-stream-get-string-length-due-to-several-conditions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
all you need to add to your calculation is to substring s by the reminder and repeat the count: -
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
s.substring(0, n % s.length()).chars().filter(x -> x == 'd').count();
EDIT: if, for some reason, you dislike the oldfashined substring, you could replace it with stream of ints from 0 to the reminder:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
IntStream.range(0, n % s.length()).filter(i -> s.charAt(i)== 'd').count();
However, the question reminas whether this all-stream version is more comprehensive/readable.
I'm was thinking that I can do that without substring, e.g some methods in stream, anyway thanks.
– Ibrahim Ali
Mar 9 at 9:52
@IbrahimAli, see my edited answer
– Sharon Ben Asher
Mar 9 at 10:08
add a comment |
all you need to add to your calculation is to substring s by the reminder and repeat the count: -
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
s.substring(0, n % s.length()).chars().filter(x -> x == 'd').count();
EDIT: if, for some reason, you dislike the oldfashined substring, you could replace it with stream of ints from 0 to the reminder:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
IntStream.range(0, n % s.length()).filter(i -> s.charAt(i)== 'd').count();
However, the question reminas whether this all-stream version is more comprehensive/readable.
I'm was thinking that I can do that without substring, e.g some methods in stream, anyway thanks.
– Ibrahim Ali
Mar 9 at 9:52
@IbrahimAli, see my edited answer
– Sharon Ben Asher
Mar 9 at 10:08
add a comment |
all you need to add to your calculation is to substring s by the reminder and repeat the count: -
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
s.substring(0, n % s.length()).chars().filter(x -> x == 'd').count();
EDIT: if, for some reason, you dislike the oldfashined substring, you could replace it with stream of ints from 0 to the reminder:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
IntStream.range(0, n % s.length()).filter(i -> s.charAt(i)== 'd').count();
However, the question reminas whether this all-stream version is more comprehensive/readable.
all you need to add to your calculation is to substring s by the reminder and repeat the count: -
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
s.substring(0, n % s.length()).chars().filter(x -> x == 'd').count();
EDIT: if, for some reason, you dislike the oldfashined substring, you could replace it with stream of ints from 0 to the reminder:
return s.chars().filter(x -> x == 'd').count() * (n / s.length()) +
IntStream.range(0, n % s.length()).filter(i -> s.charAt(i)== 'd').count();
However, the question reminas whether this all-stream version is more comprehensive/readable.
edited Mar 9 at 10:07
answered Mar 9 at 9:27
Sharon Ben AsherSharon Ben Asher
9,59932037
9,59932037
I'm was thinking that I can do that without substring, e.g some methods in stream, anyway thanks.
– Ibrahim Ali
Mar 9 at 9:52
@IbrahimAli, see my edited answer
– Sharon Ben Asher
Mar 9 at 10:08
add a comment |
I'm was thinking that I can do that without substring, e.g some methods in stream, anyway thanks.
– Ibrahim Ali
Mar 9 at 9:52
@IbrahimAli, see my edited answer
– Sharon Ben Asher
Mar 9 at 10:08
I'm was thinking that I can do that without substring, e.g some methods in stream, anyway thanks.
– Ibrahim Ali
Mar 9 at 9:52
I'm was thinking that I can do that without substring, e.g some methods in stream, anyway thanks.
– Ibrahim Ali
Mar 9 at 9:52
@IbrahimAli, see my edited answer
– Sharon Ben Asher
Mar 9 at 10:08
@IbrahimAli, see my edited answer
– Sharon Ben Asher
Mar 9 at 10:08
add a comment |
You might just be looking for a simpler logic if I get your question right. It could be as :
private int characterCountWithRecurrenceWithinLimit(String string, int limit, char c)
// repeat string unless its shorter than the limit
StringBuilder sBuilder = new StringBuilder(string);
while (sBuilder.length() < limit)
sBuilder.append(string);
// keep the string within the limit
String repeatedString = sBuilder.toString().substring(0, limit);
// count the character occurrence
return (int) IntStream.range(0, repeatedString.length())
.filter(i -> repeatedString.charAt(i) == c)
.count();
add a comment |
You might just be looking for a simpler logic if I get your question right. It could be as :
private int characterCountWithRecurrenceWithinLimit(String string, int limit, char c)
// repeat string unless its shorter than the limit
StringBuilder sBuilder = new StringBuilder(string);
while (sBuilder.length() < limit)
sBuilder.append(string);
// keep the string within the limit
String repeatedString = sBuilder.toString().substring(0, limit);
// count the character occurrence
return (int) IntStream.range(0, repeatedString.length())
.filter(i -> repeatedString.charAt(i) == c)
.count();
add a comment |
You might just be looking for a simpler logic if I get your question right. It could be as :
private int characterCountWithRecurrenceWithinLimit(String string, int limit, char c)
// repeat string unless its shorter than the limit
StringBuilder sBuilder = new StringBuilder(string);
while (sBuilder.length() < limit)
sBuilder.append(string);
// keep the string within the limit
String repeatedString = sBuilder.toString().substring(0, limit);
// count the character occurrence
return (int) IntStream.range(0, repeatedString.length())
.filter(i -> repeatedString.charAt(i) == c)
.count();
You might just be looking for a simpler logic if I get your question right. It could be as :
private int characterCountWithRecurrenceWithinLimit(String string, int limit, char c)
// repeat string unless its shorter than the limit
StringBuilder sBuilder = new StringBuilder(string);
while (sBuilder.length() < limit)
sBuilder.append(string);
// keep the string within the limit
String repeatedString = sBuilder.toString().substring(0, limit);
// count the character occurrence
return (int) IntStream.range(0, repeatedString.length())
.filter(i -> repeatedString.charAt(i) == c)
.count();
answered Mar 9 at 9:20
NamanNaman
46.4k12104206
46.4k12104206
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075732%2fjava-8-stream-get-string-length-due-to-several-conditions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown