Write code to find out the average of all values whose keys are even numbers. Round the average to two decimal places and return as output.
[Hint: If the average is 5.901, the rounded average value is 5.9. The average is 6.333, the rounded average value is 6.33. ]
The return type of the output is a floating-point value which is the average of all values whose key elements are even numbers.
Input and Output Format
- Input consists of the number of elements in the HashMap and the HashMap<int, float>.
- The output is a floating-point value that corresponds to the average.
Refer sample output for formatting specifications
Sample Input 1:
3
1
2.3
2
4.1
6
6.2
Sample Output 1:
5.15
Sample Input 2:
3
9
3.1
4
6.3
1
2.6
Sample Output 2:
6.3
Find the average of elements in Hashmap in Java
Following are the step to find the average of elements:
- Input the size of the HashMap.
- Create an empty HashMap. Add elements to it in key and value pairs.
- Pass the Hashmap to display() method.
- Inside the method, declare and initialize variable sum and count to 0.
- Use the DecimalFormat class to round the decimal place to two digits.
- Now, use the iterator to iterate over the keySet. And check if the key is even. If it is add all keys and at last take the average of them.
- At last, return the average value using the format() method.
import java.text.DecimalFormat; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = sc.nextInt(); HashMap<Integer, Float> hm = new HashMap<Integer, Float>(); for (int i = 0; i < s; i++) { hm.put((sc.nextInt()), sc.nextFloat()); } System.out.println(display(hm)); } public static String display(HashMap<Integer, Float> hm) { float sum = 0; int count = 0; DecimalFormat df = new DecimalFormat("#.00"); Iterator<Integer> it = hm.keySet().iterator(); while (it.hasNext()) { int y = it.next(); if (y % 2 == 0) { sum = (float) (sum + hm.get(y)); count++; } } float d = sum / count; return df.format(d); } }
Output
How to Sum the Values of a HashMap?
Write code to find out the sum of all values. Round the sum to two decimal places and return as output.
Input and Output Format
- Input consists of the number of elements in the HashMap and the HashMap<int, float>.
- The output is a floating-point value that corresponds to the sum.
Refer sample output for formatting specifications
Sample Input:
3
2
1.2
5
1.2
6
1.2
Sample Output:
3.60
Program to Sum the Values of a HashMap
Following are the step to find out the sum of values in a HashMap:
- Input the size of the HashMap.
- Create an empty HashMap. Add elements to it in key and value pairs.
- Pass the Hashmap to display() method.
- Inside the method, declare and initialize the variable sum to 0.
- Use the DecimalFormat class to round the decimal place to two digits.
- Use for loop, get all the values from the HashMap using values() method, and add it to the sum variable.
- At last, return the sum value using the format() method.
package com.demo3; import java.text.DecimalFormat; import java.util.HashMap; import java.util.Scanner; public class HashMapProblem { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = sc.nextInt(); HashMap<Integer, Float> hm = new HashMap<Integer, Float>(); for (int i = 0; i < s; i++) { hm.put((sc.nextInt()), sc.nextFloat()); } System.out.println(display(hm)); } public static String display(HashMap<Integer, Float> hm) { float sum = 0; DecimalFormat df = new DecimalFormat("#.00"); for(float v: hm.values()){ sum = sum + v; } return df.format(sum); } }
Output
Thus, in this way, we learn how to find the average and sum of elements in a HashMap.