Antoine Offroy
Antoine Offroy

Reputation: 43

count the number of occurences for each hour in Java

First, sorry for my English who may be poor, I hope you will understand me

I do not see how to recover my object count per hour. I hope you can help me find out more about my question.

I have a mission object that contains a mission list that each have as attribute a STRING name and a STRING time (hhmmss format)

here is an example :

0 : name1 102101

1 : name2 102801

2 : name3 104801

3 : name4 110501

4 : name5 120301

I wish I could make an array allowing me to count the number of missions for each hour

In this example I would have :

10 => 3

11 => 1

12 => 1

I do not know if you see what I would like to get :)

If you ever have small tracks I'm interested

Thank you for reading me !

I wish you a good evening

Upvotes: 0

Views: 689

Answers (1)

molamk
molamk

Reputation: 4116

TL;DR

  • As the comments mentioned, you may want to use a HashMap with String keys reflecting the hour and Integer values for the count (missions per hour).
  • Since you're dealing with hours, meaning that you have a maximum of 24 of them, you can also replace the HashMap with an Array of 24 items.

The Mission class

Basically, all is needed here is a getter for the time attribute. If you feel fancy, you can also add a getHour which will return the hour instead of the whole time string.

class Mission {
    private String name;
    private String time;
    Mission(String name, String time) {
        this.name = name;
        this.time = time;
    }

    String getHour() {
        // This gives us the 2 first characters into a String - aka the "hour"
        return time.substring(0, 2);
    }
}

Using the HashMap

We want to keep the count per hour in a HashMap. So we'll iterate over the missionsList and for each item, we'll get its count, then we'll increment it.

If the hour is not in the HashMap yet, we would normally receive a null. To handle that with minimal boilerplate, we'll use the getOrDefault method. We can call it like this map.getOrDefault("10", 0). This will return the missions count of hour 10, and if that count doesn't exist yet (which means we didn't add it to the map yet) we will receive 0 instead of null. The code will look like this

public static void main(String[] args) {
    // This will built our list of missions
    List<Mission> missionsList = Arrays.asList(
            new Mission("name1", "102101"),
            new Mission("name2", "102801"),
            new Mission("name3", "104801"),
            new Mission("name4", "110501"),
            new Mission("name5", "120301")
    );

    // This map will keep the count of missions (value) per hour (key)
    Map<String, Integer> missionsPerHour = new HashMap<>();

    for (Mission mission : missionsList) {
        // Let's start by getting the hour,
        // this will act as the key of our map entry
        String hour = mission.getHour();

        // Here we get the count of the current hour (so far).
        // This is the "value" of our map entry
        int count = missionsPerHour.getOrDefault(mission.getHour(), 0);

        // Here we increment it (by adding/replacing the entry in the map)
        missionsPerHour.put(hour, count + 1);
    }

    // Once we have the count per hour,
    // we iterate over all the keys in the map (which are the hours).
    // Then we simply print the count per hour
    for (String hour : missionsPerHour.keySet()) {
        System.out.println(String.format(
            "%s\t=>\t%d", hour, missionsPerHour.get(hour)
        ));
    }
}

Upvotes: 1

Related Questions