john
john

Reputation: 11699

How to get the count of each http status code efficiently?

I am using RestTemplate to execute the URL and then I am printing out the http status code for it.

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
System.out.println(response.getStatusCode());

Now what I need to do is, I need to get the count of each status code and store it in map as key and value. Meaning, how many times each status code is coming. If http 200 status code is coming around 100 times, then I would like to see the count of it.

I can do it by having multiple temp variables for each status code and keep on increasing the count accordingly. But apart from this is there any other easy way to do it?

Upvotes: 0

Views: 1311

Answers (4)

m4rtin
m4rtin

Reputation: 2475

Since you are actually asking for another way, you could go with an int array with the index of the int array representing the HTTP code received.

Something like :

// initialization
int[] responses = new int[600];

// for each received response
responses[response.getStatusCode().value()]++

// retrieving the number of HTTP 200 received
System.out.println("Number of HTTP 200 received : " + responses[HttpStatus.OK.value()] /* or simply responses[200] */);

Not sure what that would bring to the table though : even if it's a little faster, there's surely plenty of ints in that array that would end up wasted. Other answers detailed the Map approach which is better imho because more explicit about what you are trying to do (that is counting the number of occurrence of a particular HTTP status code). And clarity is key when writing code :)

Upvotes: 0

nem035
nem035

Reputation: 35491

Use a HashMap, then:

  • if your httpcode wasn't already in the map, insert it with count = 1.
  • if your httpcode already exist in the map, then increase its counter

    HashMap<Integer, Integer> mapCount = new HashMap<Integer, Integer>();
    
    // ...
    
    void updateMap(Integer httpCode) {
        if (!mapCount.containsKey(httpCode)) {
            mapCount.put(httpCode, 1);
        } else {
            // update counter
            int counter = mapCount.get(str).intValue() + 1;
            // overwrite existing with update counter
            mapCount.put(httpCode, counter + 1);
        }
    }
    
    // ...
    

Upvotes: 0

Cristian Sulea
Cristian Sulea

Reputation: 264

Use a Map maybe? With the status as key, and value as counter.

Map<String,Integer> counters = new HashMap<>();
...
synchronized (counters) {

  String code = response.getStatusCode();
  Integer counter = counters.get(code);

  if (counter == null) {
    counters.put(code, 1);
  } else {
    counters.put(code, counter + 1)
  }
}

Upvotes: 2

Dave
Dave

Reputation: 888

Map<Integer,Integer> statusMap = new HashMap<Integer,Integer>();

public void store(int code)
{
    if (statusMap.containsKey(code))
    {
        int value = statusMap.get(code);
        statusMap.put(code,value+1);
    }
    else
    {
        statusMap.put(code,1);      
    }
}

public void list()
{
    Iterator<Integer> iter = statusMap.keySet().iterator();
    while(iter.hasNext())
    {
        int code = iter.next();
        System.out.println(code + " : " + statusMap.get(code));
    }
}

Upvotes: 0

Related Questions