gpt4 book ai didi

java - 用于以下场景的最佳数据结构(如 HashMap /列表等)是什么

转载 作者:行者123 更新时间:2023-11-30 09:41:07 25 4
gpt4 key购买 nike

我有一个要求,比如有一个项目 A 有几个子项目,如 a1、b1、c1...,每个子项目依次有几个子项目,如 {a11、a12、a13...}对应于 a1 和 {b11,b12,b13..} 对应于 b1。所以,它基本上就像一个以项目 A 为根的树结构。现在,每个项目及其子项目都有一些时间戳。所有这些项目和子项目的时间戳都是不同的。我需要找到具有最新时间戳的项目/子项目。如何继续在 java 中解决这个问题。我对使用数据结构有点陌生。

最佳答案

使用TreeMap

它将满足您的需要。这是来自 java.samples.com 的示例程序

// Create a tree map 
TreeMap tm = new TreeMap();
// Put elements to the map
tm.put("John Doe", new Double(3434.34));
tm.put("Tom Smith", new Double(123.22));
tm.put("Jane Baker", new Double(1378.00));
tm.put("Todd Hall", new Double(99.22));
tm.put("Ralph Smith", new Double(-19.08));
// Get a set of the entries
Set set = tm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double)tm.get("John Doe")).doubleValue();
tm.put("John Doe", new Double(balance + 1000));
System.out.println("John Doe's new balance: " +
tm.get("John Doe"));

关于java - 用于以下场景的最佳数据结构(如 HashMap /列表等)是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9108000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com