简体   繁体   English

如何在 java 中创建分层数据对象

[英]How to create hierarchical data objects in java

I have received below data set from the third party database.我从第三方数据库收到了以下数据集。

AccNo  AccName  CurrentRef  PrevRef  TimeStamp
001    FCA      001         0        T00
001    FCB      002         001      T01
021    FCA      003         002      T02
011    XZA      012         0        T00
011    XZC      013         012      T01
022    YAA      021         0        T00

To identify one account we need to track all the previous reference of an account.要识别一个帐户,我们需要跟踪一个帐户的所有先前引用。 If its previous reference is 0 then it means it is the first entry of the account in data set.如果其先前的引用为 0,则表示它是数据集中帐户的第一个条目。 From the above data set,从上述数据集中,

First three rows belongs to one account.前三行属于一个帐户。 (CurrentRef - 003, its previous references - 002,001) (CurrentRef - 003,其先前的参考资料 - 002,001)

Fourth and fifth rows belong to another account.第四行和第五行属于另一个帐户。 (CurrentRef - 013, PrevRef - 012) (CurrentRef - 013, PrevRef - 012)

And so on.等等。

I need a collection which can group this data on the basis of PreviousRef so that below information can be received in a single object,我需要一个可以根据 PreviousRef 对这些数据进行分组的集合,以便可以在单个 object 中接收以下信息,

(AccountNo-latest, Complete VO (AccNo, AccName, CurrentRef, PrevRef, TimeStamp)-latest, List of Previous References)

For above data set, T02 is latest timestamp, T00 is the oldest timestamp.对于上述数据集,T02 是最新的时间戳,T00 是最旧的时间戳。 A record is latest if it's CurrentRef doesn't belong to PrevRef of any record.如果一条记录的 CurrentRef 不属于任何记录的 PrevRef,则该记录是最新的。 I need below result in a list-我需要下面的结果列表-

[021, (021, FCB, 003, 002, T02), (002,001)],
[011, (011, XZC, 013, 012, T01), (012)],
[022, (022, YAA, 021, 0, T00), ()]


  

In a comment you said there is no unique identifier for these records.在评论中,您说这些记录没有唯一标识符。 That is a problem.这是一个问题。

Not only could we be dealing with duplicate elements, but there is a larger issue.我们不仅可以处理重复的元素,而且还有一个更大的问题。

You run the possibility of infinite recursion, ie Record A's previous record is Record B and record B's previous record is Record A. Not to mention other cycles of arbitrary repetition, going forwards and backwards.您运行无限递归的可能性,即记录 A 的先前记录是记录 B,记录 B 的先前记录是记录 A。更不用说其他任意重复循环,前进和后退。

Instead of solving the problem as it is now, consider modifying your structure so there is a unique identifier.不要像现在这样解决问题,而是考虑修改你的结构,以便有一个唯一的标识符。

If there is a unique identifier, we could easily solve this by converting each record to a Linked List Node, overriding the Object.hashcode() with this unique identifier, and inserting it into a HashMap which is an O(1) operation.如果有唯一标识符,我们可以通过将每条记录转换为链接列表节点来轻松解决此问题,使用此唯一标识符覆盖 Object.hashcode(),并将其插入到 O(1) 操作的 HashMap 中。 If the previous ref is 0, we can stop here.如果之前的 ref 为 0,我们可以在这里停下来。 If not, we also want to visit the HashMap node at that previous ref and set its next to the current node.如果没有,我们还想访问前一个 ref 处的 HashMap 节点,并将其设置为当前节点的 next。 This way we have a breadcrumb trail of nodes.这样我们就有了节点的面包屑痕迹。 This is also another constant time O(1) operation.这也是另一个恒定时间 O(1) 操作。

Therefore, it follows for all records to be put into a HashMap, we spend O(n) time building the breadcrumb HashMap.因此,将所有记录放入 HashMap 中,我们花费 O(n) 时间构建面包屑 HashMap。

After, we can do another O(n) traversal to collect the desired list.之后,我们可以进行另一次 O(n) 遍历来收集所需的列表。

Something like:就像是:

List result = hashmap.filter(node -> node.previous == null).collect(Collectors.toList())

To receive a list of the origin nodes (which have links to their next refs since we built up the breadcrumb).接收原始节点的列表(自从我们建立面包屑以来,这些节点具有指向它们的下一个参考的链接)。

This is likely the cleanest and fastested possible solution in 2*O(n)=O(n) time.这可能是 2*O(n)=O(n) 时间内最干净和最快的解决方案。

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

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