Reputation: 465
I have an ArrayList
static ArrayList<term> terms = new ArrayList<term>();
and a HashMap
static HashMap<String,ArrayList<Integer>> inverted_index = new HashMap<String,ArrayList<Integer>>();
This is my class term
public class term {
int docID;
String tokenName;
public term(String tokenName, int docID)
{
this.tokenName = tokenName;
this.docID = docID;
}
public int getDocID() {
return docID;
}
public String getTokenName() {
return tokenName;
}
public String toString(){
return tokenName + " " + docID ;
}}
I want to sort it by tokenName. I did this
Collections.sort(terms, new Comparator<term>(){
public int compare(term term1, term term2){
return term1.tokenName.compareTo(term2.tokenName);
}
});
Now when I print terms, I do get the sorted order. Now I call this function
public static void createInvertedIndex(){
inverted_index.clear();
for(int i=0; i<terms.size(); i++){
ArrayList<Integer> doc_list = new ArrayList<Integer>();
if(!inverted_index.containsKey(terms.get(i).tokenName)){
doc_list.add(terms.get(i).docID);
if(i+1 < terms.size()){
if(terms.get(i).tokenName.equals(terms.get(i+1).tokenName)){
while((i+1 < terms.size()) && terms.get(i).tokenName.equals(terms.get(i+1).tokenName))
{
i++;
doc_list.add(terms.get(i).docID);
}
}
}
//System.out.println(terms.get(i)); ------ Get Sorted Terms here
inverted_index.put(terms.get(i).tokenName, doc_list);
}
}
System.out.println(inverted_index); // ------ Get Unsorted terms in this
}
I don't get sorted inverted_index. Do I need to sort it too? If so, how to do that?
I want the output in sorted order.
Upvotes: 0
Views: 99