我有一個需求,就像有一個項目A有幾個子項目,如A1,B1,C1 ...和每個子-item依次對應於a1和{b11,b12,b13 ...}的{a11,a12,a13 ...}等幾個子項對應於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"));
在JDK中實現了一個體面的樹結構。
看一看TreeModel
和TreeNode
,其目的是爲了與JTreePanel
使用,但沒有什麼使用它搖擺之外阻止你。
假設一個示例程序,沒有B項:
- 店商品A,商品B,項目C ...在一個鏈表。 (每個節點變成根)
- 項目A將具有以下子節點:a1,b1,c1 ...在鏈接列表中。 (每個節點成爲根)
- a11,a12,a13可以存儲爲a1的子節點。
- 爲B11,B12,B13類似...
現在,你在哪裏面臨的問題?
還有項目B,C ...及其子項目 – 2012-02-02 06:36:39
對於數據結構,請參閱java.util.TreeMap
獲取樹型支持的Map實現,然後java.util.TreeSet
獲取樹型支持的Set實現。這些是Java Collections API中的標準實現。
package com.mindprod.example;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import static java.lang.System.out;
/**
* Example use of java.util.TreeMap.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2010-02-25 initial version
* @see TestHashMap
* @since 2010-02-25
*/
public final class TestTreeMap
{
// --------------------------- main() method ---------------------------
/**
* Sample code to TEST TreeMap.
*
* @param args not used
*/
public static void main(String[] args)
{
// create a new HashMap
TreeMap<String, String> t = new TreeMap<String, String>(/* no size estimates needed */);
// add some key/value pairs to the TreeMap
t.put("WA", "Washington");
t.put("NY", "New York");
t.put("RI", "Rhode Island");
t.put("BC", "British Columbia");
t.put("NC", "North Carolina");
t.put("NE", "Nebraska");
// look up a key in the TreeMap
String stateName = t.get("NY");
// prints "New York"
out.println(stateName);
out.println("enumerate all the keys in the TreeMap, by key");
// keySet gives you a Set, which is not a List.
// If you need something you can sort, use toArray.
// If you need a List, then use Arrays.asList.
for (String key : t.keySet())
{
String value = t.get(key);
// prints lines of the form NY New York
// in key order, unlike a HashMap
out.println(key + " " + value);
}
out.println("enumerate all the values in the TreeMap, by key, note values out of order");
// values gives you a Collection which is not a List.
// If you need something you can sort, use to Array.
// If you need a List, then use Arrays.asList.
for (String value : t.values())
{
// prints lines of the form New York
// in key order, unlike a HashMap
out.println(value);
}
out.println("enumerate all the key/value Entries in the TreeMap, by key");
// This gives you a Map of Entry items. This is not suitable for sorting.
for (Map.Entry<String, String> entry : t.entrySet())
{
// prints lines of the form NY=New York
// in key order, unlike a HashMap
out.println("as Entry: " + entry);
// this does not require an expensive get lookup to find the value.
String key = entry.getKey();
String value = entry.getValue();
out.println("separately: " + key + " " + value);
}
out.println("extract the keys into an array");
// actual type is a private nested static class TreeMap.KeySet
// This Set is not Serializable.
Set<String> justKeys = t.keySet();
// Use toArray that takes an skeleton String[] array,
// otherwise we end up with a useless Object[] instead of a String[].
final String[] keys = justKeys.toArray(new String[ justKeys.size() ]);
out.println("extract values into an array, may contain duplicates unlike a Set.");
// the actual type is a private nested static class TreeMap.Values
// This Collection is not Serializable.
final Collection<String> justValues = t.values();
final String[] values = justValues.toArray(new String[ justValues.size() ]);
out.println("extract key/value pair entries into an array.");
final Set<Map.Entry<String, String>> justEntries = t.entrySet();
@SuppressWarnings("unchecked") final Map.Entry<String, String>[] keyValuePairs =
justEntries.toArray(new Map.Entry[ justEntries.size() ]);
// Infuriatingly, this generates an unchecked conversion warning message.
// Type erasure won't let us say:
// Map.Entry<String, String>[] keyValuePairs =
// justEntries.toArray (new Map.Entry<String,String>[justEntries.size()]);
// There might be some clever way of using Class.asSubclass to mollify the compiler.
// There so many times when generics create more problems than they solve.
}
}
您還可能有興趣在此link
我建議你創建一個項目類,那麼你可以單獨設計類。
對於子項取決於數據類型,選擇不同的數據結構。 首先,如果您知道項目的類型相同,例如a1,b1,c1都是字符串,那麼您可以使用ArrayList來保存它們,或者如果總數有界,則可以使用數組。如果它們是不同類型的,那麼考慮hashmap。
你可以簡單的使用數組
ArrayList list=new ArrayList();
ArrayList sublist=new ArrayList();
ArrayList subsublist=new ArrayList();
subsublist.add("cat");
sublist.add(subsublist);
list.add(sublist);
考慮每個項目的節點。所以創建一個類節點。這個類將有以下成員 - nodeName,parentNode,childNode。
public class Node
{
private String name;
private Node parentNode;
private Node childNode;
}
根據您的使用添加其他屬性。
寫的構造函數來創建一個Node對象如下 -
public Node(String name)
{
this.name = name;
this.parentNode = null;
this.childNode = null;
}
你需要生成getter和setter他們獲得相關的父和子節點。
我還沒有測試過代碼。請按照要求重新認證,並告知我是否可以解決您的問題。
建議實施您自己的樹結構,看起來像您的要求是自己維護一個明確的樹模型。例如,
public class Item implements Comparable<Item>{
private String name;
private long timestamp;
private List<Item> subItems = new ArrayList<Item>();
public Item(String name, long timestamp){
this.name = name;
this.timestamp = timestamp;
}
public void addItem(Item subItem){
this.subItems.add(subItem);
}
@Override
public int compareTo(Item o) {
return Long.signum(this.timestamp - o.timestamp);
}
}
如果你想獲得與最大或最小時間戳項目或對它們進行排序,你可以把所有的物品放入一個列表和排序使用Collections.sort()
,你已經實現了Comparable接口。
- 1. 哈希映射和併發哈希映射有什麼區別?
- 2. 哈希映射等效於C++
- 3. 構建數據結構 - 哈希數組的哈希哈希
- 4. 插入哈希映射數據結構JAVA
- 5. 爲什麼2D哈希映射是內存效率低下?
- 6. 如何迭代哈希映射並放入數組/列表結構?
- 7. 什麼時候是鏈表最好的數據結構使用
- 8. 通過哈希映射映射,需要返回哈希映射
- 9. 這種情況下最好的數據結構是什麼?
- 10. 這種情況下最好的數據庫結構是什麼?
- 11. 什麼是這種情況下最好的數據庫結構?
- 12. 什麼是最好的表格結構?
- 13. 如何迭代通過哈希映射映射列表元素
- 14. Clojure遍歷哈希映射列表
- 15. 如果鏈接列表和數組是基本數據結構什麼類型的數據結構是樹,哈希表,堆等?
- 16. 如何哈希值映射在Perl CGI下拉列表顯示
- 17. 哈希映射內的哈希映射的平均值
- 18. 數組結構 - 哈希表
- 19. 哈希映射,哈希集合,哈希字典之間有什麼區別?
- 20. 使用GSON的JSON反序列化從哈希映射中的哈希映射跳過數據成員
- 21. 保存哈希映射共享偏好
- 22. 什麼數據庫可用於以下場景?
- 23. 使用哈希映射
- 24. 使用哈希映射
- 25. 如何迭代哈希映射的結果,其中值是列表?
- 26. 基於java光盤的哈希映射
- 27. 有沒有什麼方法創建一個哈希映射像結構
- 28. 什麼是可以處理這些場景的好的hadoop文件夾結構?
- 29. Clojure構建2D哈希映射
- 30. 最快類型哈希映射
如果它「基本上像樹結構」,爲什麼不使用樹結構? – rtheunissen 2012-02-02 06:25:14
我們還有物品B嗎? – Azodious 2012-02-02 06:26:25
@azodious:是的,我們有項目B,C ...和他們的子項目 – 2012-02-02 06:35:36