开发者

What data structure could I use for counting occurrences of a country code?

开发者 https://www.devze.com 2023-04-06 17:55 出处:网络
I need some kind of data structure don\'t know yet which would be most sui开发者_运维知识库table.

I need some kind of data structure don't know yet which would be most sui开发者_运维知识库table.

Here is what I'm working with: I have a bunch rows of data processing and each row has its own country code.

I want to obtain as result how many times each country code repeats throughout the process.


You might try a HashMap. With a HashMap, you can use the country code as the key, and the count of how many times each appears as the value stored in that key. If you encounter a particular country code for the first time, insert it into the map with an initial value of 1; otherwise, increment the existing value.

HashMap<String, Integer> myMap = new HashMap<String, Integer>();

for (... record : records) {
    String countryCode = record.getCountryCode();

    int curVal;
    if (myMap.containsKey(countryCode)) {
        curVal = myMap.get(countryCode);
        myMap.put(countryCode, curVal + 1);
    } else {
        myMap.put(countryCode, 1);
    }
}

// myMap now contains the count of each country code, which
// can be used for whatever purpose needed.


I would use a HashMap with the country code as the key and the count as the value. Build the map from your collection and increment the count if it is already in the map.


Create a Map using country code String as the key and the current count as the value.

You realize, of course, that you can get such a thing directly out of a SQL query:

select country_code, count(country_code)
from your_table
group by country_code
order by country_code

You'll get a ResultSet with country code and count pairs. That's easy to load into a Map.


To complete the answer with something other than HashMap.

  • If your country code list is or can easily be turned into a not very sparse numeric sequence, try a int[] or long[].
  • If your country code range is sparse, but don't have many elements, create a CountryCode enum And use a EnumMap to store the amounts:

Example:

Map<CountryCode, Long> countryCodeAppearances = 
       new EnumMap<CountryCode,Long>(CountryCode.class);

Lightweight data structures will perform better and impose less memory / garbage collection overhead. So, array should be the fastest. EnumMap is kind off a hidden gem that, under the right circumstances may also give you a performance boost.


Guava offers the AtomicLongMap

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号