Figure out what is printed to the console
What will be the output of the following code ?
String letters =
{ "laid", "leave", "lean", "ease", "east", "legals", "revo", "fights", "limit", "live" };
int result = Stream.of( letters )
.filter( w -> isVowel( w.charAt( 3 ) ) )
.mapToInt( w -> w.length() )
.filter( w -> w % 2 == 0 )
.sum();
System.out.println(result);
java
add a comment |
What will be the output of the following code ?
String letters =
{ "laid", "leave", "lean", "ease", "east", "legals", "revo", "fights", "limit", "live" };
int result = Stream.of( letters )
.filter( w -> isVowel( w.charAt( 3 ) ) )
.mapToInt( w -> w.length() )
.filter( w -> w % 2 == 0 )
.sum();
System.out.println(result);
java
5
Did you try to run it?
– Eran
Nov 24 '18 at 16:05
Depends on whatisVoweldoes, you didn't include that.
– Mark
Nov 24 '18 at 16:06
18=4+6+4+4. ease, legals, revo and live has vowel at index 3 and even length. note that we exclude 'limit' because length is odd.
– boly38
Nov 24 '18 at 16:14
add a comment |
What will be the output of the following code ?
String letters =
{ "laid", "leave", "lean", "ease", "east", "legals", "revo", "fights", "limit", "live" };
int result = Stream.of( letters )
.filter( w -> isVowel( w.charAt( 3 ) ) )
.mapToInt( w -> w.length() )
.filter( w -> w % 2 == 0 )
.sum();
System.out.println(result);
java
What will be the output of the following code ?
String letters =
{ "laid", "leave", "lean", "ease", "east", "legals", "revo", "fights", "limit", "live" };
int result = Stream.of( letters )
.filter( w -> isVowel( w.charAt( 3 ) ) )
.mapToInt( w -> w.length() )
.filter( w -> w % 2 == 0 )
.sum();
System.out.println(result);
java
java
edited Nov 24 '18 at 21:17
Gayan Mettananda
1,106713
1,106713
asked Nov 24 '18 at 16:04
Anshul MadhanAnshul Madhan
1
1
5
Did you try to run it?
– Eran
Nov 24 '18 at 16:05
Depends on whatisVoweldoes, you didn't include that.
– Mark
Nov 24 '18 at 16:06
18=4+6+4+4. ease, legals, revo and live has vowel at index 3 and even length. note that we exclude 'limit' because length is odd.
– boly38
Nov 24 '18 at 16:14
add a comment |
5
Did you try to run it?
– Eran
Nov 24 '18 at 16:05
Depends on whatisVoweldoes, you didn't include that.
– Mark
Nov 24 '18 at 16:06
18=4+6+4+4. ease, legals, revo and live has vowel at index 3 and even length. note that we exclude 'limit' because length is odd.
– boly38
Nov 24 '18 at 16:14
5
5
Did you try to run it?
– Eran
Nov 24 '18 at 16:05
Did you try to run it?
– Eran
Nov 24 '18 at 16:05
Depends on what
isVowel does, you didn't include that.– Mark
Nov 24 '18 at 16:06
Depends on what
isVowel does, you didn't include that.– Mark
Nov 24 '18 at 16:06
18=4+6+4+4. ease, legals, revo and live has vowel at index 3 and even length. note that we exclude 'limit' because length is odd.
– boly38
Nov 24 '18 at 16:14
18=4+6+4+4. ease, legals, revo and live has vowel at index 3 and even length. note that we exclude 'limit' because length is odd.
– boly38
Nov 24 '18 at 16:14
add a comment |
2 Answers
2
active
oldest
votes
Here's how to find out the result without running the code:
int result = Stream.of(letters)
.filter(w -> isVowel(w.charAt(3))) // I'm assuming isVowel returns true for vowels (a,e,i,o,u),
// so after this step only the Strings whose 4th character
// (index 3) is a vowel (such as "ease") remain in the
// Stream
.mapToInt(w -> w.length()) // here you transform each remaining String to its length
// (for example "ease" becomes 4)
.filter(w -> w % 2 == 0) // here you keep only the even lengths
.sum(); // finally you sum the remaining lengths
This should be easy enough to calculate.
add a comment |
NB I'm assuming isVowel returns true if the argument is a vowel.
After executing filter(), you will end up with a list of all strings whose 4th character is a vowel.
Which means after the filter is applied you'll now have a list of
["ease", "legal", "revo", "limit", "live"]
Now your next instruction (mapToInt) says convert this list of strings to a list of integers. And how do you determine what these integers will be? Their length. The next instructions is saying creating me a list of all the lengths of the remaining strings after filtering. Which means you'll end up with a list of
[4, 5, 4, 5, 4]
Now the next operation says can you give me a list of numbers that when divided by 2 the remainder is 0? This is a list of even numbers. So the next result will be
[4, 4, 4]
Now the last instruction says, can you sum up the contents of the last evaluated list?
this is 4*3 = 12.
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%2f53459948%2ffigure-out-what-is-printed-to-the-console%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
Here's how to find out the result without running the code:
int result = Stream.of(letters)
.filter(w -> isVowel(w.charAt(3))) // I'm assuming isVowel returns true for vowels (a,e,i,o,u),
// so after this step only the Strings whose 4th character
// (index 3) is a vowel (such as "ease") remain in the
// Stream
.mapToInt(w -> w.length()) // here you transform each remaining String to its length
// (for example "ease" becomes 4)
.filter(w -> w % 2 == 0) // here you keep only the even lengths
.sum(); // finally you sum the remaining lengths
This should be easy enough to calculate.
add a comment |
Here's how to find out the result without running the code:
int result = Stream.of(letters)
.filter(w -> isVowel(w.charAt(3))) // I'm assuming isVowel returns true for vowels (a,e,i,o,u),
// so after this step only the Strings whose 4th character
// (index 3) is a vowel (such as "ease") remain in the
// Stream
.mapToInt(w -> w.length()) // here you transform each remaining String to its length
// (for example "ease" becomes 4)
.filter(w -> w % 2 == 0) // here you keep only the even lengths
.sum(); // finally you sum the remaining lengths
This should be easy enough to calculate.
add a comment |
Here's how to find out the result without running the code:
int result = Stream.of(letters)
.filter(w -> isVowel(w.charAt(3))) // I'm assuming isVowel returns true for vowels (a,e,i,o,u),
// so after this step only the Strings whose 4th character
// (index 3) is a vowel (such as "ease") remain in the
// Stream
.mapToInt(w -> w.length()) // here you transform each remaining String to its length
// (for example "ease" becomes 4)
.filter(w -> w % 2 == 0) // here you keep only the even lengths
.sum(); // finally you sum the remaining lengths
This should be easy enough to calculate.
Here's how to find out the result without running the code:
int result = Stream.of(letters)
.filter(w -> isVowel(w.charAt(3))) // I'm assuming isVowel returns true for vowels (a,e,i,o,u),
// so after this step only the Strings whose 4th character
// (index 3) is a vowel (such as "ease") remain in the
// Stream
.mapToInt(w -> w.length()) // here you transform each remaining String to its length
// (for example "ease" becomes 4)
.filter(w -> w % 2 == 0) // here you keep only the even lengths
.sum(); // finally you sum the remaining lengths
This should be easy enough to calculate.
answered Nov 24 '18 at 16:10
EranEran
287k37467556
287k37467556
add a comment |
add a comment |
NB I'm assuming isVowel returns true if the argument is a vowel.
After executing filter(), you will end up with a list of all strings whose 4th character is a vowel.
Which means after the filter is applied you'll now have a list of
["ease", "legal", "revo", "limit", "live"]
Now your next instruction (mapToInt) says convert this list of strings to a list of integers. And how do you determine what these integers will be? Their length. The next instructions is saying creating me a list of all the lengths of the remaining strings after filtering. Which means you'll end up with a list of
[4, 5, 4, 5, 4]
Now the next operation says can you give me a list of numbers that when divided by 2 the remainder is 0? This is a list of even numbers. So the next result will be
[4, 4, 4]
Now the last instruction says, can you sum up the contents of the last evaluated list?
this is 4*3 = 12.
add a comment |
NB I'm assuming isVowel returns true if the argument is a vowel.
After executing filter(), you will end up with a list of all strings whose 4th character is a vowel.
Which means after the filter is applied you'll now have a list of
["ease", "legal", "revo", "limit", "live"]
Now your next instruction (mapToInt) says convert this list of strings to a list of integers. And how do you determine what these integers will be? Their length. The next instructions is saying creating me a list of all the lengths of the remaining strings after filtering. Which means you'll end up with a list of
[4, 5, 4, 5, 4]
Now the next operation says can you give me a list of numbers that when divided by 2 the remainder is 0? This is a list of even numbers. So the next result will be
[4, 4, 4]
Now the last instruction says, can you sum up the contents of the last evaluated list?
this is 4*3 = 12.
add a comment |
NB I'm assuming isVowel returns true if the argument is a vowel.
After executing filter(), you will end up with a list of all strings whose 4th character is a vowel.
Which means after the filter is applied you'll now have a list of
["ease", "legal", "revo", "limit", "live"]
Now your next instruction (mapToInt) says convert this list of strings to a list of integers. And how do you determine what these integers will be? Their length. The next instructions is saying creating me a list of all the lengths of the remaining strings after filtering. Which means you'll end up with a list of
[4, 5, 4, 5, 4]
Now the next operation says can you give me a list of numbers that when divided by 2 the remainder is 0? This is a list of even numbers. So the next result will be
[4, 4, 4]
Now the last instruction says, can you sum up the contents of the last evaluated list?
this is 4*3 = 12.
NB I'm assuming isVowel returns true if the argument is a vowel.
After executing filter(), you will end up with a list of all strings whose 4th character is a vowel.
Which means after the filter is applied you'll now have a list of
["ease", "legal", "revo", "limit", "live"]
Now your next instruction (mapToInt) says convert this list of strings to a list of integers. And how do you determine what these integers will be? Their length. The next instructions is saying creating me a list of all the lengths of the remaining strings after filtering. Which means you'll end up with a list of
[4, 5, 4, 5, 4]
Now the next operation says can you give me a list of numbers that when divided by 2 the remainder is 0? This is a list of even numbers. So the next result will be
[4, 4, 4]
Now the last instruction says, can you sum up the contents of the last evaluated list?
this is 4*3 = 12.
answered Nov 24 '18 at 16:39
ivange94ivange94
4461514
4461514
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%2f53459948%2ffigure-out-what-is-printed-to-the-console%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
5
Did you try to run it?
– Eran
Nov 24 '18 at 16:05
Depends on what
isVoweldoes, you didn't include that.– Mark
Nov 24 '18 at 16:06
18=4+6+4+4. ease, legals, revo and live has vowel at index 3 and even length. note that we exclude 'limit' because length is odd.
– boly38
Nov 24 '18 at 16:14