简体   繁体   English

使用时间线存储数据范围的数据结构

[英]Data structure for storing ranges of data with timeline

I need to store the activity log data for users in my application. 我需要在我的应用程序中存储用户的活动日志数据。 It includes the the time when the user started doing an activity. 它包括用户开始进行活动的时间。 For eg, at 1 PM, the started 'Activity A'. 例如,在下午1点启动的“活动A”。 At 2 PM, the user started 'Activity B'. 在下午2点,用户启动了“活动B”。 At 3 PM, the user started 'Avtivity A'. 下午3点,用户启动“ Avtivity A”。 From these logs, I should later be able to query information like 从这些日志中,我以后应该能够查询诸如

  1. How long was the user doing Activity A between 12 AM to 8 PM? 用户在上午12点到晚上8点之间进行活动A多长时间了?
  2. How many times did the user switch activities etc. 用户切换了几次活动等。

Can anyone suggest what would be a good in-memory data structure to store such information? 谁能建议一个好的内存数据结构来存储此类信息?

Edit: There could be thousands of logs. 编辑:可能有数千条日志。 A user can't do multiple activities at the same time. 用户不能同时进行多项活动。

First, let's try to have a data structure for you to make the query assuming you have a single activity. 首先,假设您只有一个活动,让我们尝试使用一个数据结构来进行查询。

So let's have a list to store the timestamps (I am considering an integer example). 因此,让我们有一个列表来存储时间戳(我正在考虑一个整数示例)。

ArrayList<Integer> integers = new ArrayList<Integer>();

In this, you can store the timestamps. 在此,您可以存储时间戳。 Let's say you have stored 假设您已经存储了

integers.add(1);
integers.add(10);
integers.add(12);
integers.add(176);
integers.add(1678);

Now you need to way to access the only specific set of values in it. 现在,您需要一种方法来访问其中唯一的一组特定值。 Consider the NavigableSet class from JDK. 考虑一下JDK中的NavigableSet类。

NavigableSet<Integer> set = new TreeSet<>(integers);

You can query specific set using 您可以使用查询特定集合

set.subSet(0, 175); // this will give 1,10,12 from the above values

Now Have a map to store multiple Activities. 现在有一张地图来存储多个活动。

Map<String, List> activityTimeStampmap= new HashMap<>();

You can add Activity if it does not exist, You have to update the list if Activity Already exists. 您可以添加活动(如果不存在)。如果活动已经存在,则必须更新列表。

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM