用于以下场景的最佳数据结构(如哈希映射/列表等)是什么
我有一个要求,比如有一个项目 A,它有几个子项目,如 a1,b1,c1...,每个子项目又有几个子项目,如 {a11,a12,a13...} 对应于a1 和 {b11,b12,b13..} 对应于 b1。所以,它基本上就像一个以项目 A 为根的树结构。现在,每个项目及其子项目都有一些关联的时间戳。所有这些项目和子项目的时间戳都不同。我需要找到具有最新时间戳的项目/子项目。如何在java中继续解决这个问题。我对使用数据结构有点陌生。
I have a requirement like there is an item A that has several sub items like a1,b1,c1... and each sub-item has in turn several sub-items like {a11,a12,a13...} which correspond to a1 and {b11,b12,b13..} which correspond to b1. So, its basically like a tree structure with item A as the root. Now, there is some time-stamp associated with each item and its sub-items. The time-stamp is different for all these items and sub-items. I need to find the item/sub-item with the latest time-stamp. How to proceed to solve this in java. Im kind of new to using data structures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 TreeMap
它会满足您的需要。这是来自 java.samples.com 的示例程序
Use TreeMap
It will suit your need. Here is a sample program from java.samples.com
对于数据结构,请查看
java.util.TreeMap
以了解树支持的 Map 实现,并查看java.util.TreeSet
以了解树支持的 Set 实现。这些是 Java Collections API 中的标准实现。您可能也对此链接感兴趣
For data structures, take a look at
java.util.TreeMap
for a tree-backed Map implementation, andjava.util.TreeSet
for a tree-backed Set implementation. These are standard implementations found in the Java Collections API.You may also be interested in this link
JDK 中实现了一个不错的树结构。
查看
TreeModel
和TreeNode
,它们设计为与JTreePanel
一起使用,但没有什么可以阻止您在 Swing 之外使用它。There is a decent tree structure implemented in the JDK.
Have a look at
TreeModel
andTreeNode
, which are designed to be used with aJTreePanel
but there is nothing stopping you from using it outside of Swing.我建议您为 Item 创建一个类,然后您可以单独设计该类。
对于子项取决于数据类型,选择不同的数据结构。
首先,如果您知道项目具有相同的类型,例如 a1、b1、c1 都是字符串,那么您可以使用 ArrayList 来保存它们,或者如果总数有界,则可以使用数组。如果类型不同,就考虑hashmap。
I recommend you to create a Class for Item, then you can design the class separately.
For subitems depends on the data types, choose different data structures.
First if you know that items are of same types, for example a1, b1, c1 are all string then you can use ArrayList to hold them, or use array if the total number is bounded. If they are of different types, consider hashmap then.
您可以简单地使用数组
You can simple use an array
假设不存在B项:
现在,您在哪里面临问题?
Assuming, there is not item B:
Now, where are you facing problem?
将每个项目视为一个节点。所以创建一个Node类。这个班级将有以下成员 -
节点名称、父节点、子节点。
根据您的用途添加附加属性。
编写构造函数来创建 Node 对象,如下所示 -
您将需要为它们生成 getter 和 setter,以获取关联的父节点和子节点。
我没有测试过代码。请根据要求重新格式化它,并让我知道它是否解决了您的问题。
Consider every item as a Node. So make a class Node. This class will have following members -
nodeName, parentNode, childNode.
Add additional attributes according to your use.
Write the constructor to create a Node object as below -
You will need to generate getters and setters for them to get the parent and child nodes associated.
I have not tested the code. Please re-fornat it as per the requirement and let me know if it resolves your issue.
建议实现您自己的树结构,因为看起来您的要求是自己维护一个显式的树模型。例如,
如果您想获取时间戳最大或最小的项目或对它们进行排序,您可以将所有项目放入一个 List 中并使用
Collections.sort()
进行排序,因为您已经实现了 Comparable 接口。Suggest to implement your own Tree Structure, as looks like your requirement is to maintain an explicit tree model yourself. For example,
If you want to get the item with largest or smallest timestamp or sort them, you can put all the items into a List and sort using
Collections.sort()
as you have implemented the Comparable interface.