Data aggregation involves compiling and summarizing data from various sources into a unified format. Using custom functions, you can tailor the aggregation process to meet specific analytical needs, allowing for more flexible and insightful data analysis.
In this example, we will create a custom function to sum all even numbers in a list. This demonstrates how custom functions can be used to perform specific data aggregation tasks.
import java.util.*;
class SumEvenNumbers {
public static int sumEven(List numbers) {
return numbers.stream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum();
}
public static void main(String args[]) {
List numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
System.out.println("Sum of even numbers: " + sumEven(numbers));
}
}
The custom function sumEven
uses Java Streams to filter and sum even numbers. This approach is efficient and concise, leveraging functional programming paradigms.
Console Output:
Sum of even numbers: 12
This example illustrates how to calculate the average of positive numbers using a custom aggregation function. Aggregating data in this way allows you to focus on specific subsets of data.
import java.util.*;
class AveragePositiveNumbers {
public static double averagePositive(List numbers) {
return numbers.stream().filter(n -> n > 0).mapToInt(Integer::intValue).average().orElse(0);
}
public static void main(String args[]) {
List numbers = Arrays.asList(-1, 2, -3, 4, 5);
System.out.println("Average of positive numbers: " + averagePositive(numbers));
}
}
The averagePositive
function filters out non-positive numbers and calculates the average of the remaining numbers. The use of Streams makes the code both readable and efficient.
Console Output:
Average of positive numbers: 3.6666666666666665
Finding the maximum value in a dataset is a common aggregation task. This example shows how to use a custom function to achieve this.
import java.util.*;
class MaxValueFinder {
public static int findMax(List numbers) {
return numbers.stream().mapToInt(Integer::intValue).max().orElse(Integer.MIN_VALUE);
}
public static void main(String args[]) {
List numbers = Arrays.asList(1, 2, 3, 4, 5);
System.out.println("Maximum value: " + findMax(numbers));
}
}
The findMax
function maps each number to an integer and finds the maximum value. Using Streams, we can easily perform such operations with minimal code.
Console Output:
Maximum value: 5
Calculating the median of a dataset is another useful aggregation task. This example demonstrates how to implement a custom function for calculating the median.
import java.util.*;
class MedianCalculator {
public static double calculateMedian(List numbers) {
Collections.sort(numbers);
int size = numbers.size();
if (size % 2 == 0) {
return (numbers.get(size/2 - 1) + numbers.get(size/2)) / 2.0;
} else {
return numbers.get(size/2);
}
}
public static void main(String args[]) {
List numbers = Arrays.asList(3, 1, 4, 1, 5, 9);
System.out.println("Median: " + calculateMedian(numbers));
}
}
The calculateMedian
function sorts the list and then determines the median based on the list's size. This method provides a straightforward approach to finding the median.
Console Output:
Median: 3.5
Variance is a measure of how much values in a dataset differ from the mean. This example shows how to create a custom function to calculate variance.
import java.util.*;
class VarianceCalculator {
public static double calculateVariance(List numbers) {
double mean = numbers.stream().mapToInt(Integer::intValue).average().orElse(0);
return numbers.stream().mapToDouble(n -> Math.pow(n - mean, 2)).average().orElse(0);
}
public static void main(String args[]) {
List numbers = Arrays.asList(1, 2, 3, 4, 5);
System.out.println("Variance: " + calculateVariance(numbers));
}
}
The calculateVariance
function computes the mean and then calculates the variance by averaging the squared differences from the mean. This approach is efficient and clear.
Console Output:
Variance: 2.0
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies