Question: Hello, please help me understand these codes public static void countFrequencies(ArrayList list) { // hashmap to store the frequency of element Map hm = new

Hello, please help me understand these codes

public static void countFrequencies(ArrayList list)

{

// hashmap to store the frequency of element

Map hm = new HashMap();

for (String i : list) {

Integer j = hm.get(i);

hm.put(i, (j == null) ? 1 : j + 1);

}

// displaying the occurrence of elements in the arraylist

for (Map.Entry val : hm.entrySet()) {

System.out.println("Element " + val.getKey() + " "

+ "occurs"

+ ": " + val.getValue() + " times");

}

}

Can someone explain to me what this for loop

for (String i : list) {

Integer j = hm.get(i);

hm.put(i, (j == null) ? 1 : j + 1);

}

does, and this for loop does?

for (Map.Entry val : hm.entrySet()) {

System.out.println("Element " + val.getKey() + " "

+ "occurs"

+ ": " + val.getValue() + " times");

}

}

I found these on a website but i am having a hard time to understand what these does.

Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!