开发者

Sorting an ArrayList<String> in a TreeMap

开发者 https://www.devze.com 2022-12-17 20:36 出处:网络
I am piping in a file. I am tracking word pairs from the file. Using a treemap the keys are all sorted. However, when i add words to those keys they are not sorted.

I am piping in a file. I am tracking word pairs from the file. Using a treemap the keys are all sorted. However, when i add words to those keys they are not sorted. here is the part i need help on in the process function:

 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 is sorted

nextWord is not..

Can i use Collections.sort(result); somehow? im just not sure how i get to the nextWord within the result to do that. or, is there no way to do it within my situation. I would rather not change things unless you recommend it.

This is the program

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 = i开发者_如何学PythonnputWords.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();
}
}


If you want the collection of "nextword"s sorted, why not use a TreeSet rather than an ArrayList? The only reason I can see against it is if you might have duplicates. If duplicates are allowed, then yes, use Collections.sort on the ArrayList when you're done adding to them. Or look in the Apache Commons or Google collection classes - I don't know them off the top of my head, but I'm sure there is a sorted List that allows duplicates in one or both of them.


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


Y Don't you try some thing like this

Collections.sort(inputWords);

0

精彩评论

暂无评论...
验证码 换一张
取 消