队列和映射类型功能的混合

发布于 2024-10-25 06:30:09 字数 705 浏览 1 评论 0原文

据我所知,队列是一种通过头部和/或尾部访问的集合类型。

我还了解映射是一种集合类型,包含许多唯一的键,每个键都有一个关联的值。

我需要的是一个具有固定数量值的地图,其中的键将被设置一次,并且在首次设置后不会更改。

我无法理解的困难部分是让这些值充当具有固定长度的队列,可以将其推送,而不影响密钥。

例如:

初始集合(顶部的最新项目):

Key1, Value1
Key2, Value2
Key3, Value3

添加新值后的集合 NewValue:

Key1, NewValue
Key2, Value1
Key3, Value2

因此,我希望键保持不变,并且能够通过将最旧的值推到末尾来添加新值,并且将所有其他值进一步向下移动到最后。

如果框架内没有这样的类型,有人可以推荐一种实现它的方法吗?

谢谢你!

编辑: 为什么我需要这样做的一个例子:

我需要能够访问这些值 根据当时的时间段 创建于。
示例:

键:0 将包含今天的值
键:-1 将包含昨天的值
键:-2 将包含两天前的值
等等

每次仅输入一个值 天,并且一个值将始终是 每天都进入。

I understand that a queue is a collection type that is accessed through the head and/or tail.

I also understand that a map is a collection type the contains a number of unique keys, which each have an associated value.

What I need, is a map, with a fixed number of values, where the keys will be set once, and will not be changed after they are first set.

The hard part I can't get my head around is getting the values to act as a queue with a fixed length, which can be pushed, without affecting the key.

For example:

Initial collection (newest items at the top):

Key1, Value1
Key2, Value2
Key3, Value3

Collection after adding new value NewValue:

Key1, NewValue
Key2, Value1
Key3, Value2

So, I want the key to stay the same, and be able to add the new value, by pushing the oldest value off the end, and moving all other values further down towards the end.

If a type like this isn't available within the framework, could somebody please recommend a way to implement it.

Thank you!

EDIT:
An example of why I need to do this:

I need to be able to access the values
according to the time period it was
created in.
Example:

Key: 0 will contain a value from today
Key: -1 will contain a value from yesterday
Key: -2 will contain a value from two days ago
etc.

Only one value will be entered every
day, and one value will ALWAYS be
entered every day.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随遇而安 2024-11-01 06:30:10

听起来您可以使用:

new LinkedHashMap<Long,V>(initialCapacity, loadFactor, false) {
    protected boolean removeEldestEntry(Map.Entry<Long,V> e) {
        return size() > MAX_ENTRIES;
    }
};

要在该地图中获取或放置项目,您需要规范化您的时间戳,例如当天的 00:00:00.000。

这种方法的一个优点是,当违反“每天都会输入一个值”的假设时,它不会默默地失败。

Sounds like you could use:

new LinkedHashMap<Long,V>(initialCapacity, loadFactor, false) {
    protected boolean removeEldestEntry(Map.Entry<Long,V> e) {
        return size() > MAX_ENTRIES;
    }
};

To get or put items in this map you will need to canonicalize your timestamp, e.g., to 00:00:00.000 of the day.

An advantage of this approach is when the assumption that "one value will ALWAYS be entered every day" is violated it won't fail silently.

喜爱皱眉﹌ 2024-11-01 06:30:10

所以,你实际上是在谈论数组的索引。

我只是将“ArrayList”包装在您的对象中。

您的 push 方法还会从末尾删除“最旧”的元素。您可以定义诸如 getToday()getPriorDay(numDaysBackFromToday) 之类的方法。

但是,如果您实际上没有存储日期,那么您会在其中使用大量幻数或数据的时间。您希望数据已正确加载。

就我个人而言,我会使用与 LinkedHashMap 中的数据关联的时间戳并迭代以找到所需的时间戳。

So, you're really talking about an index into an array.

I'd just wrap an 'ArrayList` in your object.

Your push method would also remove the "oldest" element from the end. You'd define methods such as getToday() and getPriorDay(numDaysBackFromToday)

However, you're using a lot of magic numbers there if you're not actually storing a date or a time with the data. You're hoping that the data has been loaded correctly.

Personally I'd use a timestamp associated with the data in a LinkedHashMap and iterate to find the one you want.

不语却知心 2024-11-01 06:30:10

我重新思考了有关幻数的问题,并决定使用带有正索引的预定义集合类型会更容易。然后,当我达到所需的最大值数时,我将简单地让 Push 方法将最后一个项目从末尾删除。

然后我会让我的类将 -1 解释为 1 以获得所需的值。

谢谢你的帮助,我只是想太多了。

I've rethought my question with regards to the magic numbers, and have decided it'd be easier to use a predefined collection type with positive indexes. Then I'll simply make the push method drop the last item off the end when I reach the maximum number of values I need.

Then I'll get my class to interpret -1 as 1 to get the required value.

Thanks for your help, I was simply over-thinking things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文