• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing Odd Numbers

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What problem you are facing?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: https://coderanch.com/t/631548/java/java/Lambda-FizzBuzz#3028752

In short, this is a pain.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would look like this:
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you do need your extended class with the else method.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with using Collectors.partitioningBy(predicate, reducer)?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right; the sums are non‑final, so that won't work.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ew :P
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Ew :P

Is that worse than my -99%?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot all about summingInt()!

Piet was right.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Partitioning rather than grouping? Of course. Well done PS
 
reply
    Bookmark Topic Watch Topic
  • New Topic