2012-02-02 16 views
2

我有一個需求,就像有一個項目A有幾個子項目,如A1,B1,C1 ...和每個子-item依次對應於a1和{b11,b12,b13 ...}的{a11,a12,a13 ...}等幾個子項對應於b1。所以,它基本上就像一個以項目A爲根的樹結構。現在,每個項目及其子項目都有一些時間戳記。所有這些項目和子項目的時間戳都是不同的。我需要找到帶有最新時間戳的項目/子項目。如何繼續在java中解決這個問題。我對使用數據結構很陌生。什麼是最好的數據結構(如哈希映射/列表等)用於以下場景

+0

如果它「基本上像樹結構」,爲什麼不使用樹結構? – rtheunissen 2012-02-02 06:25:14

+0

我們還有物品B嗎? – Azodious 2012-02-02 06:26:25

+0

@azodious:是的,我們有項目B,C ...和他們的子項目 – 2012-02-02 06:35:36

回答

1

使用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")); 
0

在JDK中實現了一個體面的樹結構。

看一看TreeModelTreeNode,其目的是爲了與JTreePanel使用,但沒有什麼使用它搖擺之外阻止你。

0

假設一個示例程序,沒有B項:

  1. 店商品A,商品B,項目C ...在一個鏈表。 (每個節點變成根)
  2. 項目A將具有以下子節點:a1,b1,c1 ...在鏈接列表中。 (每個節點成爲根)
  3. a11,a12,a13可以存儲爲a1的子節點。
  4. 爲B11,B12,B13類似...

現在,你在哪裏面臨的問題?

+0

還有項目B,C ...及其子項目 – 2012-02-02 06:36:39

1

對於數據結構,請參閱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

0

我建議你創建一個項目類,那麼你可以單獨設計類。

對於子項取決於數據類型,選擇不同的數據結構。 首先,如果您知道項目的類型相同,例如a1,b1,c1都是字符串,那麼您可以使用ArrayList來保存它們,或者如果總數有界,則可以使用數組。如果它們是不同類型的,那麼考慮hashmap。

0

你可以簡單的使用數組

ArrayList list=new ArrayList(); 
ArrayList sublist=new ArrayList(); 
ArrayList subsublist=new ArrayList(); 
subsublist.add("cat"); 
sublist.add(subsublist); 
list.add(sublist); 
0

考慮每個項目的節點。所以創建一個類節點。這個類將有以下成員 - 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他們獲得相關的父和子節點。

我還沒有測試過代碼。請按照要求重新認證,並告知我是否可以解決您的問題。

0

建議實施您自己的樹結構,看起來像您的要求是自己維護一個明確的樹模型。例如,

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接口。

相關問題