Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Is filter an aggregate operation?

 
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Is the filter method in stream api an aggregate function?

2. Shouldn't an aggregate function return a single value like the sum operation?
 
Bartender
Posts: 370
44
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Yes, it is applied to each element of the stream

2. No, I think you're thinking of a reduction operation
 
Marshal
Posts: 74696
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look in the Java™ Tutorials about that sort of question. That section defines what aggregate operations are, and describes filter() and forEach().
 
Suhaas Parekh
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between aggregate and reduction operation? What are all the examples for the same?
 
Suhaas Parekh
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember while writing sql queries we used the sum operation along with the group by clause. In that case we used to consider sum as an aggregate operation, while in java sum is considered as a reduction. Why, the difference?
 
Campbell Ritchie
Marshal
Posts: 74696
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People use different words in different languages.
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Look in the Java™ Tutorials about that sort of question. That section defines what aggregate operations are, and describes filter() and forEach().



Let's look at how the terms "aggregate" and "reduction" are used in that tutorial:

Java Tutorial wrote:with a pipeline that consists of the aggregate operations filter, mapToInt, and average:
...
The JDK contains many terminal operations such as average that return one value by combining the contents of a stream. These operations are called reduction operations


So which one is average, aggregate or reduction? I think the confusion stems from thinking aggregate operations are separate from reduction operations. They're not; they're just two different ways of describing what operations do. They are not necessarily mutually exclusive.

Intermediate operations and terminal operations are mutually exclusive. The filter operation is an intermediate aggregate operation. It returns another stream. The forEach operation is a terminal aggregate operation. The average operation is a terminal aggregate operation because it processes elements from a stream. It's also a reduction operation because it returns a single value by combining the elements of the stream it processed.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic