2010-03-10 33 views
1

怎樣才能讓你從fileID高效many-to-many -relation到WordswordfileIDs沒有數據庫-tools一樣的Postgres在Java中?有高效率的許多一對多關係中的Java

我有以下類。 從fileIDwords的關係很便宜,但不是相反的,因爲我需要三個for環回。

alt text http://img191.imageshack.us/img191/4077/oliorakenne1.png

我的解決方案顯然不是有效的。 其他選項可能會創建一個具有word作爲ID的012xx的fileID s的額外類。

回覆JacobM的回答

MyFile的的構造函數的相關部分是:

  /** 
      * Synopsis of data in wordToWordConutInFile.txt: 
      * fileID|wordID|wordCount 
      * 
      * Synopsis of the data in the file wordToWordID.txt: 
      * word|wordID 
      **/   


    /** 
    * Getting words by getting first wordIDs from wordToWordCountInFile.txt and then words in wordToWordID.txt. 
    */ 
    InputStream in2 = new FileInputStream("/home/dev/wordToWordCountInFile.txt"); 
    BufferedReader fi2 = new BufferedReader(new InputStreamReader(in2)); 

    ArrayList<Integer> wordIDs = new ArrayList<Integer>(); 
    String line = null; 
    while ((line = fi2.readLine()) != null) { 
     if ((new Integer(line.split("|")[0]) == currentFileID)) { 
      wordIDs.add(new Integer(line.split("|")[6])); 
     } 
    } 
    in2.close(); 

    // Getting now the words by wordIDs. 
    InputStream in3 = new FileInputStream("/home/dev/wordToWordID.txt"); 
    BufferedReader fi3 = new BufferedReader(new InputStreamReader(in3)); 

    line = null; 
    while ((line = fi3.readLine()) != null) { 
     for (Integer wordID : wordIDs) { 
      if (wordID == (new Integer(line.split("|")[1]))) { 
       this.words.add(new Word(new String(line.split("|")[0]), fileID)); 
       break; 
      } 
     } 
    } 
    in3.close(); 

    this.words.addAll(words); 

字的構造是the paste

回答

1

在知道Word在文件中的位置,從Word到MyFile分配鏈接是否更有效?也就是說,你如何構建MyFile對象中的單詞列表?如果您從文件系統中讀取MyFile中的文件,而不是讀取每個單詞中的文件,則將其MyFile分配給當前文件。

//within MyFile constructor or setter for Words 
while (//there's another word to add) { 
    Word newWord = new Word(//read word from file); 
    words.add(newWord); 
    newWord.setMyFile(this); 
} 

這類似於管理雙向父子關係的典型方式:

//in Parent 
public void addChild(Child child) { 
    myChildren.add(child); 
    child.setParent(this); 
} 

如果你告訴我們,你如何建立MyFile的對象時,它可能會有所幫助。

編輯您補充說,建立詞彙列表中的代碼之後:

好了,已經看到,建立你的話,我不認爲建立關係是你的效率低​​下的源代碼。看起來你正在按照我建議的方式建立關係(當你添加每個單詞時,你將該單詞作爲相應文件的fileID)。

看起來效率低下的原因在於,對於每個單詞,您必須將它與當前在一組文件中的各種內容(例如WordToWordId)進行匹配。因此,對於每一個單詞,你必須遍歷該文件的每一行,並找到匹配。這當然是低效的。

更好的方法是將這些配對存儲在HashMap的內存中,在啓動時初始化。這樣,如果你有一個特定的單詞並需要相應的ID,反之亦然,你可以在你的HashMap中查找它,這是一個常量操作。同樣,對於每個單詞,您都循環遍歷每個文件;再次,循環一次,並將結果存儲在一個HashMap中。然後查找變得不變。

+0

我添加了MyFile的consructors到我的問題。 –

+0

好的,我看到構造函數,但是我仍然沒有看到單詞列表的填充位置。這就是我感興趣的內容。 –

+0

謝謝你指出!我在我的問題中添加了構造函數的相關部分。 –

1

這兩個類都應該重寫hashCode和equals。因此你會決定什麼是平等的。

然後,您將在每個類中創建一個集合。

public class MyFile implements Comparable<MyFile> { 
    //your fields here 
    Set<Word> words = new HashSet<Word>(0); 
    //Remember to override hashCode and equals 
} 

public class Word implements Comparable<Word> { 
    //your fields here 
    Set<MyFile> words = new HashSet<MyFile>(0); 
    //Remember to override hashCode and equals 
} 

在你的套現在你將所有的MyFiles.words和otherway周圍,所有的Words.myFile

+0

這也可以與@JacobM所說的 –

0

我想你想要的文件知道它的字和詞知道文件在那裏用來。

public class File { 

private List<Word> words; 
public File(){ 
words=new Vector<Word>(); 
} 

/** 
*The method add word to word list. 
**/ 
public addWord(Word word){ 
this.words.add(word); 
word.addFile(this); 
} 
} 
public class Word{ 
List<File> files; 
public addFile(File file){ 
this.files.add(file); 
} 
} 

反之亦然...但你應該質疑GRASP設計pattern.Maybe你的數據類型是錯誤的(我不說的不對,因爲ITIS你德興,所以我尊重)。

+0

結合使用是的,這是我的錯誤。我現在修好了 –