2010-01-21 88 views
0

我在一個文件中管道。我正在跟蹤文件中的單詞對。使用樹形圖的鍵都是排序的。但是,當我向這些鍵添加單詞時,他們沒有排序。 這裏就是我需要幫助就在這個過程中的功能部件:在TreeMap中對ArrayList <String>排序

private static void process(){ 


if(!result.containsKey(thisWord)){ 
      result.put(thisWord, new ArrayList<String>()); 

     } 

     // Add nextWord to the list of adjacent words to thisWord: 
     result.get(thisWord).add(nextWord); // nextword is not sorted within the key 

thisword排序

nextWord不..

我可以使用Collections.sort(結果);不知何故? 即時通訊不知道如何到結果內的下一個詞做到這一點。 或者,在我的情況下沒有辦法做到這一點。除非你推薦它,否則我寧願不改變它。

這是程序

import java.util.Map.Entry; 
import java.util.TreeSet; 
import java.io.*; 
import java.util.*; 





public class program1 { 

private static List<String> inputWords = new ArrayList<String>(); 
private static Map<String, List<String>> result = new TreeMap<String, List<String>>(); 



public static void main(String[] args) { 


    collectInput(); 
    process(); 
    generateOutput(); 
} 


private static void collectInput(){ 
    Scanner  sc = new Scanner(System.in);  
    String  word; 


    while (sc.hasNext()) {      // is there another word? 
     word = sc.next();      // get next word 
     if (word.equals("---")) 
     { 
      break; 
      } 

     inputWords.add(word); 

     } 

} 

private static void process(){ 


    // Iterate through every word in our input list 
    for(int i = 0; i < inputWords.size() - 1; i++){ 

     // Create references to this word and next word: 
     String thisWord = inputWords.get(i); 
     String nextWord = inputWords.get(i+1); 


     // If this word is not in the result Map yet, 
     // then add it and create a new empy list for it. 
     if(!result.containsKey(thisWord)){ 
      result.put(thisWord, new ArrayList<String>()); 

     } 

     // Add nextWord to the list of adjacent words to thisWord: 
     result.get(thisWord).add(nextWord); // need to sort nextword 
     // Collections.sort(result); 

    } 

} 


private static void generateOutput() 
    { 

    for(Entry e : result.entrySet()){ 
     System.out.println(e.getKey() + ":"); 

     // Count the number of unique instances in the list: 
     Map<String, Integer> count = new HashMap<String, Integer>(); 
     List<String> words = (List)e.getValue(); 
     for(String s : words){ 
      if(!count.containsKey(s)){ 
       count.put(s, 1); 
      } 
      else{ 
       count.put(s, count.get(s) + 1); 
      } 
     } 

     // Print the occurances of following symbols: 
     for(Entry f : count.entrySet()){ 
      System.out.println("  " + f.getKey() + ", " + f.getValue()); 

     } 
    } 
    System.out.println(); 
} 
} 
+0

我頭地圖的地圖將工作良好我不知道如何我可以添加文件到地圖的地圖.. – Steller 2010-01-21 06:26:36

回答

0
result.get(thisWord).add(nextWord); 
Collections.sort(result.get(thisWord)); 
+0

我需要nextWord排序。 – Steller 2010-01-21 03:48:36

1

如果你想要的 「nextword」 S分類收集,爲什麼不使用一個TreeSet,而不是一個ArrayList?我能看到的唯一原因是如果你可能有重複。如果允許重複,則是,在完成添加時,使用ArrayList上的Collections.sort。或者查看Apache Commons或Google collection類 - 我不知道它們是否在我頭頂,但我確定有一個已排序的List,允許其中一個或兩個都有重複。

+0

我不知道如何使用TreeSet代替arraylist? – Steller 2010-01-21 03:50:46

+0

我試過了: private static Map > result = new TreeMap >(); 和result.put(thisWord,new TreeSet ()); 當我嘗試運行該程序時出現錯誤:TreeSet無法轉換爲列表。 – Steller 2010-01-21 03:59:15

+0

你是否試圖將result.get()轉換爲List。 ? 這對我有用:result.get(thisword).add(nextword); – Nrj 2010-01-21 09:28:09

0

Y你不試試這樣的事嗎

Collections.sort(inputWords);

+0

,因爲它然後它將關鍵字排序到下一個單詞和配對的單詞不正確 – Steller 2010-01-21 05:49:10