Data Aggregation in Java using Collections Framework
Last Updated :
15 Sep, 2022
Let's look at the problem before understanding what data aggregation is. You are given daily consumption of different types of candies by a candy-lover. The input is given as follows.
Input Table:
Data Before Aggregation
Desired Output Table:
Data After Aggregation
Above is the aggregated data. Now let's understand What is Data Aggregation?
What is Data Aggregation?
Let's understand it with an example. For instance, in the input table, we are given the consumption amount of different kinds of candies each day. For example, on Aug 28, 2022, the volunteer consumed only 2 kinds of candies which are KitKat and Hershey's. Whereas, on Aug 29, 2022, the volunteer consumed 4 kinds of candies which are KitKat, Skittles, Alpen Liebe and Cadbury. Now, we are also given the consumption amount. For instance, the most eaten candy on Aug 29, 2022, is Cadbury. Whereas, the most eaten candy on Aug 28, 2022, is KitKat. However, looking at the input table, we cannot directly answer the following question: Which candy is the most popular each day (or even which candy is popular overall)?
Now, it seems like looking at the above input table we can answer the question by immediately looking over the data for matching dates. But imagine, we run the survey for a month or a quarter and we now introduce 100 more brands of candies for the volunteer to choose and eat from. The size of the data will grow so quickly, that it would be almost impossible to answer the question just by looking at the table. There's even another possibility where the data is scattered such that the data collected for a specific date is not shown consecutively as shown in the input table above. In that case, it would become even more complicated to directly look at the raw data and answer.
Now to answer such statistical questions in an efficient manner, we would need to organize our data. We would need to categorize the data in such a way that by looking at our transformed data, we can immediately answer the question that is:- which candy is more popular each day? For instance, by looking at the data after aggregation, we can say that on August 28, KitKat is more eaten and on Aug 29, Cadbury is more eaten just by looking at the column under each date. Not only that, but we can now also answer the following questions:
- On what date was a particular kind of candy eaten more? (By looking at the row of that candy)
- Which candy is popular overall? (By looking at the last "Total" column).
- Which day witnessed the most candy consumption? (By looking at the last "Total" row)
For example,Â
- Alpenliebe was eaten more on Aug 27 and Aug 29.Â
- Kitkat on the other hand is the overall popular candy.Â
- Aug 29, turned out to be the day when most candies were consumed. Maybe, we can declare it "Candy Day".
So, we are experiencing the benefits of aggregating the data. It's a technique of summarizing the data we have for the purpose of analyzing it, making the raw data more meaningful. We are now in a more efficient position to answer the above questions.
Problem Statement
We are required to transform the given input table of candy consumption on a specific date into an aggregated table where data collected for each candy should be aggregated into a value for a day. (Refer to the output table above). Following is the code for the above problem: Â Â Â
Java
import java.util.*;
class CandyConsumption {
String date;
String candy;
int consumption;
CandyConsumption(String date, String candy, int consumption){
this.date = date;
this.candy = candy;
this.consumption = consumption;
}
public String toString(){
StringBuffer str = new StringBuffer();
str.append( date );
str.append( "\t\t\t\t" );
str.append( String.valueOf( candy ) );
str.append( "\t\t\t\t" );
str.append( String.format("%20s", String.valueOf( consumption ) ));
return str.toString() ;
}
public static void main(String[] args){
CandyConsumption[] cc = new CandyConsumption[9];
cc[0] = new CandyConsumption("27-08-2022", "skittles", 20);
cc[1] = new CandyConsumption("27-08-2022", "Kitkat", 10);
cc[2] = new CandyConsumption("27-08-2022", "Alpenliebe", 20);
cc[3] = new CandyConsumption("28-08-2022", "Kitkat", 30);
cc[4] = new CandyConsumption("28-08-2022", "Hershey's", 25);
cc[5] = new CandyConsumption("29-08-2022", "Kitkat", 30);
cc[6] = new CandyConsumption("29-08-2022", "skittles", 15);
cc[7] = new CandyConsumption("29-08-2022", "Alpenliebe", 20);
cc[8] = new CandyConsumption("29-08-2022", "Cadbury", 45);
// Before Aggregation
System.out.println("Date\t\t\t\t\tCandy\t\t\t\tConsumption");
for( int i = 0 ; i < cc.length ; i++ ) {
System.out.println(cc[i]) ;
}
System.out.println();
System.out.println();
System.out.println("After Aggregation");
System.out.println();
// After aggregation
aggregate(cc);
}
public static void aggregate(CandyConsumption[] cc){
// Key => Candy Column (a/c to output table) |
// Value = Another HashMap which maps each date
// to the amount of candies consumed on that date
HashMap<String, HashMap<String, Integer>> map = new HashMap<>();
// An arraylist to store unique dates
ArrayList<String> dates = new ArrayList<>();
// HashMap to calculate total consumption datewise
// Key => Date | Value => Total number of
// candies consumed on that Date
HashMap<String, Integer> consumptionDatewise = new HashMap<>();
// HashMap to calculate total consumption candywise
// Key => Candy | Value => Total number of candies
// consumed of that Candy type
HashMap<String, Integer> consumptionCandywise = new HashMap<>();
// Populate map HashMap
for(int i=0;i<cc.length;i++){
String date = cc[i].date;
String candy = cc[i].candy;
int consumption = cc[i].consumption;
if(!map.containsKey(candy)){
map.put(candy, new HashMap<>());
}
map.get(candy).put(date, consumption);
// Let's also populate the dates
// arraylist simultaneously
if(!dates.contains(date)){
dates.add(date);
}
// Let's also populate the
// consumptionDatewise hashmap
if(!consumptionDatewise.containsKey(date)){
consumptionDatewise.put(date, 0);
}
consumptionDatewise.put(date, consumptionDatewise.getOrDefault(date, 0) + consumption);
}
// We have calculated total consumption datewise.
// Let's now calculate the total consumption
// of each candy
for(String candy : map.keySet()){
HashMap<String, Integer> candyVal = map.get(candy);
int total = 0;
for(String date : candyVal.keySet()){
total += candyVal.get(date);
}
consumptionCandywise.put(candy, total);
}
// We are done with all the necessary pre-processing.
// Let's start printing.
// Let's print the Header Line first
System.out.print(String.format("%-15s", "Candy/Date"));
for(String date : dates){
System.out.print(date + "\t");
}
System.out.println("Total");
// Printing the rest of the table
for(String candy : map.keySet()){
// System.out.printf("%-4s", candy);
System.out.print(String.format("%-15s" , candy));
HashMap<String, Integer> candyVal = map.get(candy);
for(int I = 0; I < dates.size(); i++){
if(!candyVal.containsKey(dates.get(i)))
System.out.print("0" + "\t\t");
else
System.out.print(candyVal.get(dates.get(i)) + "\t\t");
}
// Finally printing the total candywise
System.out.println(consumptionCandywise.get(candy));
}
// Printing the Total consumption datewise :- Last Line
System.out.print(String.format("%-15s", "Total"));
int total = 0;
for(int i=0;i<dates.size();i++){
int candiesOnDate = consumptionDatewise.get(dates.get(i));
total += candiesOnDate;
System.out.print(candiesOnDate + "\t\t");
}
System.out.println(total);
}
}
Output:
Date Candy Consumption
27-08-2022 skittles 20
27-08-2022 Kitkat 10
27-08-2022 Alpenliebe 20
28-08-2022 Kitkat 30
28-08-2022 Hershey's 25
29-08-2022 Kitkat 30
29-08-2022 skittles 15
29-08-2022 Alpenliebe 20
29-08-2022 Cadbury 45
After Aggregation
Candy/Date 27-08-2022 28-08-2022 29-08-2022 Total
Kitkat 10 30 30 70
Cadbury 0 0 45 45
Alpenliebe 20 0 20 40
Hershey's 0 25 0 25
skittles 20 0 15 35
Total 50 55 110 215
Similar Reads
How to Get a Size of Collection in Java?
Given a Collection in Java, the task is to find the length or size of the collection. Examples: Input: Array_List: [1, 2, 3,4] Output: 4 Input: Linked_List: [geeks, for, geeks] Output: 3 The Size of the different collections can be found with the size() method. This method returns the number of elem
2 min read
Output of Java Programs | Set 55 (Java Collections Framework)
Pre-requisites: Java Collection Framework. 1. What is the Output of following Java Program? Java import java.util.*; class Demo { public static void main(String[] args) { ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(11); arr.add(2); arr.add(3); arr.add(5); arr.add(7); arr.r
6 min read
How to Compare two Collections in Java?
Java Collection provides an architecture to store and manipulate the group of objects. Here we will see how to compare Elements in a Collection in Java. Steps: Take both inputs with help of asList() function.Sort them using Collections.sort() method.Compare them using equals() function.Print output.
2 min read
Java Program to Add the Data from the Specified Collection in the Current Collection
The grouping of objects in a single unit is called a collection. For example Array, Lists, Hashsets, etc. Data can be added from a specified collection in the current collection by using the addAll() method in Java. This method falls under the header file, java.util.*. The addAll() method returns a
3 min read
Convert an Array into Collection in Java
Java Collection provides an architecture to store and manipulate a group of objects. The datatype of data can be changed to a general data type like array into Collection. To convert array-based data into Collection based we can use java. util. Arrays class. This class provides a static method asLis
4 min read
AbstractCollection retainAll() Method in Java
The retainAll() method in AbstractCollection is used to retain only the elements that are present in a specified collection. It removes all other elements that do not match. This method is useful when performing set intersection operations in Java collections.Example 1: This example demonstrates how
2 min read
Java Collection Programs - Basic to Advanced
As it cleared from its name itself "Collection" it is a pre-defined collective bunch of classes and Interfaces present in the "Collection Framework" in Java. Their Classes, Interfaces and Methods are frequently used in competitive programming. This article provides a variety of programs on Java Coll
4 min read
How to Convert a HashTable to Other Collections Types in Java?
In Java, a Hashtable is a data structure that stores the data in the form of key and value pairs. And each key is mapped to a specific value. It implements the Map interface. HashTable provides a simple way to access the data using the unique keys. In this article, we will learn how to convert a Has
3 min read
Java Collection Exercise
The Java Collection framework is a fundamental part of the Java programming language, It covers a major part of Java and acts as a prerequisite for many Advanced Java Topics. However, programmers often find it difficult to find a platform to Practice Java Online. Take a look at our Java Collection E
11 min read
Java Program to Find the Intersection Between Two Collection
Collection means a set of different classes and interfaces are group together into a single unit that has similar functions are called collection and framework we know very that provides a predefined architecture to represents and manipulate collections in java. Here we will be discussing discuss ou
4 min read