2010-01-21 3 views
0

파일로 파이핑 중입니다. 파일에서 단어 쌍을 추적 중입니다. 트리 맵을 사용하면 키가 모두 정렬됩니다. 그러나, 내가 그 열쇠에 단어를 추가 할 때 그들은 정렬되지 않습니다.ArrayList 배열 <String>을 트리 맵에 배열

난은, Collections.sort (결과)를 사용할 수 있습니다 ..

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가 아닌 정렬되며, 여기 는 내가 처리 기능에에 도움이 필요한 부분입니다 어쩐지? 메신저 그냥 결과 내에서 nextWord가 어떻게되는지 확신 할 수 없습니다. 또는 내 상황에서이를 수행 할 방법이 없습니다. 나는 당신이 그것을 추천하지 않으면 오히려 사물을 바꾸지 않을 것입니다. 당신이 분류 "nextword"의 수집을 원하는 경우에

이 프로그램

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

답변

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

다음 단어를 정렬해야합니다. – Steller

1

이유는 ArrayList를 대신 TreeSet의를 사용하지입니까? 내가 반대 할 수있는 유일한 이유는 당신이 중복을 가질 수 있다는 것입니다. 중복이 허용되면, 예를 들어, 추가가 끝나면 ArrayList에 Collections.sort를 사용하십시오. 아니면 Apache Commons 또는 Google 콜렉션 클래스를 살펴보십시오. 나는 머리 꼭대기에서 그것들을 모르지만, 하나 또는 둘 모두에서 중복을 허용하는 정렬 된 목록이있을 것이라고 확신합니다.

+0

arraylist 대신 TreeSet을 어떻게 사용할 수 있을지 모르겠습니다. – Steller

+0

I TRIED : private static Map > 결과 = 새 TreeMap >(); 및 result.put (thisWord, new TreeSet ()); 프로그램을 실행하려고하면 오류가 발생합니다. TreeSet을 목록으로 형변환 할 수 없습니다. – Steller

+0

result.get()을 List로 유형 변환하려고합니다. ? 이것이 저에게 효과적입니다 : result.get (thisword) .add (nextword); – Nrj

0

Y이 같은 것을 시도하지 마십시오

Collections.sort (입력 단어);

+0

다음 단어를 다음 단어로 정렬하고 쌍으로 된 단어가 잘못 되었기 때문에 – Steller