2010-01-21 90 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