Math.toIntExact inside lambda expression?
I'm learning about lambda expressions.
Given a list of names, I want to count the numbers of names that start with N
.
I did that:
final static List<String> friends = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");
public static int countFriendsStartWithN() {
return Math.toIntExact(friends
.stream()
.filter(name -> name.startsWith("N"))
.count());
}
The call to the count method returns a primitive long
but I want an int
.
I used Math.toIntExact
to get the long
value as int
.
Is it possible to get the int
value directly inside the lambda expression?
java lambda
add a comment |
I'm learning about lambda expressions.
Given a list of names, I want to count the numbers of names that start with N
.
I did that:
final static List<String> friends = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");
public static int countFriendsStartWithN() {
return Math.toIntExact(friends
.stream()
.filter(name -> name.startsWith("N"))
.count());
}
The call to the count method returns a primitive long
but I want an int
.
I used Math.toIntExact
to get the long
value as int
.
Is it possible to get the int
value directly inside the lambda expression?
java lambda
What do you mean by lambda experssion? That looks alright to me.
– SamzSakerz
Nov 16 '18 at 8:10
3
If you know the long value is not too large, you can simply cast it toint
.
– Eran
Nov 16 '18 at 8:11
2
@Eran and you know it's not too large, because it is necessarily no greater than the number of items in a collection, which is anint
.
– Andy Turner
Nov 16 '18 at 8:14
I just want to know if is possible to do the int conversion inside the stream process.
– Jesus Zavarce
Nov 16 '18 at 8:16
add a comment |
I'm learning about lambda expressions.
Given a list of names, I want to count the numbers of names that start with N
.
I did that:
final static List<String> friends = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");
public static int countFriendsStartWithN() {
return Math.toIntExact(friends
.stream()
.filter(name -> name.startsWith("N"))
.count());
}
The call to the count method returns a primitive long
but I want an int
.
I used Math.toIntExact
to get the long
value as int
.
Is it possible to get the int
value directly inside the lambda expression?
java lambda
I'm learning about lambda expressions.
Given a list of names, I want to count the numbers of names that start with N
.
I did that:
final static List<String> friends = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");
public static int countFriendsStartWithN() {
return Math.toIntExact(friends
.stream()
.filter(name -> name.startsWith("N"))
.count());
}
The call to the count method returns a primitive long
but I want an int
.
I used Math.toIntExact
to get the long
value as int
.
Is it possible to get the int
value directly inside the lambda expression?
java lambda
java lambda
edited Nov 16 '18 at 8:38
Jesus Zavarce
asked Nov 16 '18 at 8:09
Jesus ZavarceJesus Zavarce
1,069819
1,069819
What do you mean by lambda experssion? That looks alright to me.
– SamzSakerz
Nov 16 '18 at 8:10
3
If you know the long value is not too large, you can simply cast it toint
.
– Eran
Nov 16 '18 at 8:11
2
@Eran and you know it's not too large, because it is necessarily no greater than the number of items in a collection, which is anint
.
– Andy Turner
Nov 16 '18 at 8:14
I just want to know if is possible to do the int conversion inside the stream process.
– Jesus Zavarce
Nov 16 '18 at 8:16
add a comment |
What do you mean by lambda experssion? That looks alright to me.
– SamzSakerz
Nov 16 '18 at 8:10
3
If you know the long value is not too large, you can simply cast it toint
.
– Eran
Nov 16 '18 at 8:11
2
@Eran and you know it's not too large, because it is necessarily no greater than the number of items in a collection, which is anint
.
– Andy Turner
Nov 16 '18 at 8:14
I just want to know if is possible to do the int conversion inside the stream process.
– Jesus Zavarce
Nov 16 '18 at 8:16
What do you mean by lambda experssion? That looks alright to me.
– SamzSakerz
Nov 16 '18 at 8:10
What do you mean by lambda experssion? That looks alright to me.
– SamzSakerz
Nov 16 '18 at 8:10
3
3
If you know the long value is not too large, you can simply cast it to
int
.– Eran
Nov 16 '18 at 8:11
If you know the long value is not too large, you can simply cast it to
int
.– Eran
Nov 16 '18 at 8:11
2
2
@Eran and you know it's not too large, because it is necessarily no greater than the number of items in a collection, which is an
int
.– Andy Turner
Nov 16 '18 at 8:14
@Eran and you know it's not too large, because it is necessarily no greater than the number of items in a collection, which is an
int
.– Andy Turner
Nov 16 '18 at 8:14
I just want to know if is possible to do the int conversion inside the stream process.
– Jesus Zavarce
Nov 16 '18 at 8:16
I just want to know if is possible to do the int conversion inside the stream process.
– Jesus Zavarce
Nov 16 '18 at 8:16
add a comment |
5 Answers
5
active
oldest
votes
No, it is not possible to fit your call to toIntExact
into your chain of method calls, your stream pipeline. This is because count
is a terminal operation and returns a primitive long
on which no method call is possible. A terminal operation is an operation that ends the stream pipeline and produces a result (or a side effect).
So I believe the best thing you can do is to live with the code you already have. IMHO it’s fine.
add a comment |
Well, here's a somewhat silly way of calculating the count as an int without casting:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.mapToInt (s -> 1)
.sum();
}
add a comment |
You can't do anything inside the lambda expression you currently have, since that's a Predicate: it returns a boolean. Math.toIntExact
returns an int
.
You can do it without the Math.toIntExact
(or a simple cast) like so:
return /* create stream, filter */
.mapToInt(a -> 1).sum();
But this is likely to be slower than doing what you are doing at the moment.
add a comment |
Yet another option that is not really better - it is possible to use a collector that applies a finisher:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.collect(Collectors.collectingAndThen(Collectors.counting(), Math::toIntExact));
}
This may have an advantage if you need it frequenty - you could build a utility method returning this Collector
to make it reusable.
add a comment |
Here's a way to do this with reduce
public static int countFriendsStartWithN2() {
return friends
.stream()
.filter(name -> name.startsWith("N"))
.map(s -> 1)
.reduce(0, Integer::sum);
}
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%2f53333790%2fmath-tointexact-inside-lambda-expression%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, it is not possible to fit your call to toIntExact
into your chain of method calls, your stream pipeline. This is because count
is a terminal operation and returns a primitive long
on which no method call is possible. A terminal operation is an operation that ends the stream pipeline and produces a result (or a side effect).
So I believe the best thing you can do is to live with the code you already have. IMHO it’s fine.
add a comment |
No, it is not possible to fit your call to toIntExact
into your chain of method calls, your stream pipeline. This is because count
is a terminal operation and returns a primitive long
on which no method call is possible. A terminal operation is an operation that ends the stream pipeline and produces a result (or a side effect).
So I believe the best thing you can do is to live with the code you already have. IMHO it’s fine.
add a comment |
No, it is not possible to fit your call to toIntExact
into your chain of method calls, your stream pipeline. This is because count
is a terminal operation and returns a primitive long
on which no method call is possible. A terminal operation is an operation that ends the stream pipeline and produces a result (or a side effect).
So I believe the best thing you can do is to live with the code you already have. IMHO it’s fine.
No, it is not possible to fit your call to toIntExact
into your chain of method calls, your stream pipeline. This is because count
is a terminal operation and returns a primitive long
on which no method call is possible. A terminal operation is an operation that ends the stream pipeline and produces a result (or a side effect).
So I believe the best thing you can do is to live with the code you already have. IMHO it’s fine.
answered Nov 16 '18 at 8:14
Ole V.V.Ole V.V.
31.8k74257
31.8k74257
add a comment |
add a comment |
Well, here's a somewhat silly way of calculating the count as an int without casting:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.mapToInt (s -> 1)
.sum();
}
add a comment |
Well, here's a somewhat silly way of calculating the count as an int without casting:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.mapToInt (s -> 1)
.sum();
}
add a comment |
Well, here's a somewhat silly way of calculating the count as an int without casting:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.mapToInt (s -> 1)
.sum();
}
Well, here's a somewhat silly way of calculating the count as an int without casting:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.mapToInt (s -> 1)
.sum();
}
answered Nov 16 '18 at 8:18
EranEran
290k37478563
290k37478563
add a comment |
add a comment |
You can't do anything inside the lambda expression you currently have, since that's a Predicate: it returns a boolean. Math.toIntExact
returns an int
.
You can do it without the Math.toIntExact
(or a simple cast) like so:
return /* create stream, filter */
.mapToInt(a -> 1).sum();
But this is likely to be slower than doing what you are doing at the moment.
add a comment |
You can't do anything inside the lambda expression you currently have, since that's a Predicate: it returns a boolean. Math.toIntExact
returns an int
.
You can do it without the Math.toIntExact
(or a simple cast) like so:
return /* create stream, filter */
.mapToInt(a -> 1).sum();
But this is likely to be slower than doing what you are doing at the moment.
add a comment |
You can't do anything inside the lambda expression you currently have, since that's a Predicate: it returns a boolean. Math.toIntExact
returns an int
.
You can do it without the Math.toIntExact
(or a simple cast) like so:
return /* create stream, filter */
.mapToInt(a -> 1).sum();
But this is likely to be slower than doing what you are doing at the moment.
You can't do anything inside the lambda expression you currently have, since that's a Predicate: it returns a boolean. Math.toIntExact
returns an int
.
You can do it without the Math.toIntExact
(or a simple cast) like so:
return /* create stream, filter */
.mapToInt(a -> 1).sum();
But this is likely to be slower than doing what you are doing at the moment.
answered Nov 16 '18 at 8:18
Andy TurnerAndy Turner
84k983143
84k983143
add a comment |
add a comment |
Yet another option that is not really better - it is possible to use a collector that applies a finisher:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.collect(Collectors.collectingAndThen(Collectors.counting(), Math::toIntExact));
}
This may have an advantage if you need it frequenty - you could build a utility method returning this Collector
to make it reusable.
add a comment |
Yet another option that is not really better - it is possible to use a collector that applies a finisher:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.collect(Collectors.collectingAndThen(Collectors.counting(), Math::toIntExact));
}
This may have an advantage if you need it frequenty - you could build a utility method returning this Collector
to make it reusable.
add a comment |
Yet another option that is not really better - it is possible to use a collector that applies a finisher:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.collect(Collectors.collectingAndThen(Collectors.counting(), Math::toIntExact));
}
This may have an advantage if you need it frequenty - you could build a utility method returning this Collector
to make it reusable.
Yet another option that is not really better - it is possible to use a collector that applies a finisher:
public static int countFriendsStartWithN() {
return friends.stream()
.filter(name -> name.startsWith("N"))
.collect(Collectors.collectingAndThen(Collectors.counting(), Math::toIntExact));
}
This may have an advantage if you need it frequenty - you could build a utility method returning this Collector
to make it reusable.
edited Nov 16 '18 at 9:01
answered Nov 16 '18 at 8:56
HulkHulk
3,42812142
3,42812142
add a comment |
add a comment |
Here's a way to do this with reduce
public static int countFriendsStartWithN2() {
return friends
.stream()
.filter(name -> name.startsWith("N"))
.map(s -> 1)
.reduce(0, Integer::sum);
}
add a comment |
Here's a way to do this with reduce
public static int countFriendsStartWithN2() {
return friends
.stream()
.filter(name -> name.startsWith("N"))
.map(s -> 1)
.reduce(0, Integer::sum);
}
add a comment |
Here's a way to do this with reduce
public static int countFriendsStartWithN2() {
return friends
.stream()
.filter(name -> name.startsWith("N"))
.map(s -> 1)
.reduce(0, Integer::sum);
}
Here's a way to do this with reduce
public static int countFriendsStartWithN2() {
return friends
.stream()
.filter(name -> name.startsWith("N"))
.map(s -> 1)
.reduce(0, Integer::sum);
}
answered Nov 16 '18 at 8:25
SamzSakerzSamzSakerz
1,6691627
1,6691627
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%2f53333790%2fmath-tointexact-inside-lambda-expression%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
What do you mean by lambda experssion? That looks alright to me.
– SamzSakerz
Nov 16 '18 at 8:10
3
If you know the long value is not too large, you can simply cast it to
int
.– Eran
Nov 16 '18 at 8:11
2
@Eran and you know it's not too large, because it is necessarily no greater than the number of items in a collection, which is an
int
.– Andy Turner
Nov 16 '18 at 8:14
I just want to know if is possible to do the int conversion inside the stream process.
– Jesus Zavarce
Nov 16 '18 at 8:16