Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing Odd Numbers  RSS feed

 
Alicia Ferraro
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently writing a program that lists numbers the user enters and totals the sums of all, even and odd using an array. When calculating the sum of the even numbers the odd numbers in the list should be removed. Here is the code:



I am able to add the even numbers but I am having trouble remove the odd numbers. Any tips?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problem you are facing?
 
Stephan van Hulst
Saloon Keeper
Posts: 7712
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to remove the numbers, all you have to do is use IntStream.filter() and IntStream.sum() twice.

If you want to do this in one pass, you can also use one loop over the numbers array, and keep two accumulator variables for the odd and even numbers.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you partition the stream into two and add odd and even in one pass?

Duplicating discussion in our Java8 forum.
 
Stephan van Hulst
Saloon Keeper
Posts: 7712
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Can you partition the stream into two and add odd and even in one pass?


Not easily. Since you can only call one terminal operation per stream, you'd have to extend IntStream with a reduceIf(IntPredicate, int, IntBinaryOperator) method, that returns a ConditionallyReducingIntStream, which has an elseReduce(int, IntBinaryOperator) method, which returns an IntStream of reduced values on which you can perform the terminal operation.

For a concrete example, check this post: http://www.coderanch.com/t/631548/java/java/Lambda-FizzBuzz#3028752

In short, this is a pain.
 
Stephan van Hulst
Saloon Keeper
Posts: 7712
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would look like this:
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you do need your extended class with the else method.
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with using Collectors.partitioningBy(predicate, reducer)?
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise you would end up with some horror like this?I presume you have come across the bitwise and operator as an alternative to the remainder operator? Once you have tried Stream operations with int[] arrays, you will start cursing the very existence of arrays of primitives. I am assuming that numbers is a List<Integer>, in which case how do you get Integer::parseInt to compile? I am allowing the compiler to do its own unnboxing rather than trying to do it explicitly with map methods.

Can you use Collectors#groupingBy to create a Map<Integer, Integer> with remainders and totals?
 
Stephan van Hulst
Saloon Keeper
Posts: 7712
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with using a collector is that you still have to pass the elements twice, which is what Campbell's initial question was about (I think). Once to partition, and once to process.

Campbell, I'm not sure you code compiles, because your lambda closes over non-final variables it attempts to assign values to.

As for primitive arrays and primitive streams:
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right; the sums are non‑final, so that won't work.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a local Sum class which is mutable so its instances can be final. You can get that to compile, surely. You too can get -99% for code style
 
Stephan van Hulst
Saloon Keeper
Posts: 7712
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ew :P
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Ew :P
Is that worse than my -99%?
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:What's wrong with using Collectors.partitioningBy(predicate, reducer)?

I think this is the best advice so far. Untested:
 
Stephan van Hulst
Saloon Keeper
Posts: 7712
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot all about summingInt()!

Piet was right.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partitioning rather than grouping? Of course. Well done PS
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!