开发者

Java HashMap, count Same key entries

开发者 https://www.devze.com 2023-04-12 08:13 出处:网络
so I\'m wo开发者_JAVA技巧rking on an android project, and right now, I\'m dynamically populating a calendar I created. Currently, as each cell in dynamically generated, it Opens the database, does a c

so I'm wo开发者_JAVA技巧rking on an android project, and right now, I'm dynamically populating a calendar I created. Currently, as each cell in dynamically generated, it Opens the database, does a call to do a count of how many entries for a certain date, then closes the database.

This means that every time the calendar is loaded, or the user changes the month, its doing a minimum 28, maximum 42 database Open/Close calls to populate the view.

To get around this, I thought of using a hash map, do one database call that populates a cursor, and traverse the cursor, pop it into a Hashmap.

It seems to me, the easiest way to do it, would be use the formatted date from the database as a key for the HashMap, and then when I need to know how many entries, is it possible to know how many entries there are with the same key?

If not, whats best, my back up thought is...

Traverse the cursor, Does the Key Exist? -> No, then create it with a int value of one -> Yes, then increment the value of that entry by 1?

Thanks guys!


For getting all duplicate entries, just retrieve the List of key and process by using Set or HashSet. You can refer to this question: Java: Detect duplicates in ArrayList?


I think I would modify my query to group by the date, and then drop those results in a hash map. So, assuming you have some event table (this is oracle syntax)

  select to_char(event.date, 'yyyymmdd'), count(*)
  from event
  group by to_char(event.date, 'yyyymmdd')

Then, you could drop that in a hash map or iterate over the result set.

0

精彩评论

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

关注公众号