计算 15 分钟内按下按钮的次数
我的应用程序有 24 个按钮来计算不同的车辆类型和方向(该应用程序将用于计算流量)。目前,每次按下按钮时,我都会将一行保存到 .csv 文件中。 csv 文件包含时间戳、方向等。
我必须测量 15 分钟间隔内每个按钮被按下的次数。
我应该如何存储按钮的计数器? 我只需要输出 15 分钟内每个按钮(每个按钮都有不同的标签用于识别)被按下的频率。
我正在考虑使用 HashMap,它可以将按钮的标签作为键,将出现的次数作为值,如下所示:
HashMap<String, Integer> hm = new HashMap<String, Integer>();
Integer value = (Integer) hm.get(buttonTag);
if (value == null) {
hm.put(buttonTag, 1);
} else {
int nr_occ = value.intValue() + 1;
hm.put(buttonTag, nr_occ);
}
但是,我不需要按钮按下的总和,而是需要 15 分钟的时间块。由于我已经将时间戳写入原始 csv 文件,我想知道应该如何存储这些值。
编辑:我目前正在测试 HashMap,它的计数效果非常好,但我看到两个问题:首先,将其分组为 15 分钟的间隔,其次,哈希图没有以任何方式排序。我需要输出按车辆类型和方向排序的值。
My application has 24 buttons to count different vehicle types and directions (the app will be used to count traffic). Currently, I'm saving a line to a .csv file each time a button is pressed. The csv file contains a timestamp, direction, etc.
I have to measure how many times every button was pressed in 15-minute intervals.
How should I store the counters for the buttons?
I just need to output how often every button (every button has a different tag for identification) was pressed in 15 minutes.
I was thinking about using a HashMap, which could just take the button's tag as key and the number of occurrences as value, like this:
HashMap<String, Integer> hm = new HashMap<String, Integer>();
Integer value = (Integer) hm.get(buttonTag);
if (value == null) {
hm.put(buttonTag, 1);
} else {
int nr_occ = value.intValue() + 1;
hm.put(buttonTag, nr_occ);
}
However, I don't need the total sums of button presses, but in 15 minute chunks. And as I'm already writing timestamps to the raw csv-file, I'm wondering how I should store these values.
Edit: I'm currently testing the HashMap, and it's working really well for counting, but I see two issues: first of all, grouping it into 15-minute intervals and secondly, the hashmap isn't sorted in any way. I'd need to output the values sorted by vehicle types and directions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的数据很简单(即您只需要一个键/值对),请考虑使用 SharedPreferences 来存储按钮 id 和按下的时间。
这是一个好方法,因为它非常快。您已经将信息放入 .csv 文件中,但提取数据并遍历它以便可以比较时间戳在我看来开销太大。
当按下按钮时,将数据存储在 .csv 文件中,然后还存储键/值(id/时间戳),然后您可以迭代它并进行比较并输出您需要输出的任何内容。
另一种方法(可能甚至更好)是简单地创建并写入 .csv 文件,然后转储它并使用其他更强大的方法来处理该数据,因为无论如何您可能都会这样做。
编辑:我看到很多答案都说使用 SQLite、静态等...
这些对于你所要求的都是较差的方法。这就是为什么...
对于简单的按钮 ID 和时间戳来说,SQLite 实在是太多了。但是,如果您认为将来可能需要存储更多数据,这实际上可能是一个选择。
如果系统碰巧销毁了 Activity,则静态变量可能会被销毁。
If your data is simple (i.e. you only need a key/value pair) then consider using SharedPreferences to store the button id and the time it was pressed.
This is a good way because it is extremely fast. You already put the info into a .csv file but to extract the data and traverse through it so that you can compare timestamps is too much overhead IMO.
When a button is pressed store your data in the .csv file then also store the key/value (id/timestamp) then you can iterate through that and do your compare and output whatever you need to output.
The other way (and probably even better) is to simply create and write to your .csv file, then dump it and use something else more robust to process that data as you will probably be doing this anyway.
EDIT: I see a lot of answers which are saying to use SQLite, statics, etc...
These are all inferior methods for what you are asking. Here is why...
SQLite is WAY too much for just a simple button id and timestamp. However if you think you might need to store more data in the future this may in fact be an option.
A static variable might be destroyed if the system happens to destroy the Activity.
如果您想变得酷,只需在每次您的 Activity 被销毁 (
onDestroy
) 时序列化HashMap
并在您的 Activity 被销毁时加载它即可创建(onCreate
)。这将是最快且最简单的方法。您将数据序列化到哪里?到
Environment.getExternalStorage()
中的文件。哦,您可能想要跟踪时间戳以每 15 分钟清除一次
HashMap
- 您可以将其放入SharedPreferences
中。我假设您已经知道如何执行此操作,但以防万一:Java 序列化
If you want to be cool, just serialise the
HashMap
every time your activity gets destroyed (onDestroy
) and load it back up when your activity gets created (onCreate
). This is going to be the quickest and simplest way.Where are you serializing your data to? To a file in
Environment.getExternalStorage()
.Oh and you might want to keep track of a timestamp to clear the
HashMap
every 15 minutes - you could put this inSharedPreferences
.I assume you already know how to do this but just in case: Java Serialization
可能是我,但这听起来是使用 SQLite 数据库的理想情况。因此,您可以通过选择时间戳轻松地从中获取间隔。
Might be me but this sounds as an ideal situation for using a SQLite database. So you can easily get this interval thing from that by selecting on the timestamp.