I have two lists: 1. with words 2. with respective frequency counts
Now I want to sort both of the list in descending order so that index of a word in the first list matches to that of the second list containing frequency counts, respectively.
Adding a function:
public String[] process() throws Exception
{
String[] ret = new String[20];
int c=0;
BufferedReader br = new BufferedReader(new FileReader(inputFileName));
String line = br.readLine();
List<String> result = new ArrayList<String>();
List<Integer> sorted = new ArrayList<Integer>();
List<String> key= new ArrayList<String>();
List<String> new_list = new ArrayList<String>();
int x=0;
while(line!=null){
StringTokenizer st = new StringTokenizer(line,delimiters);
String token = "";
while (st.hasMoreTokens()) {
token = st.nextToken();
//System.out.println(token);
if(token!=null)
{
//System.out.println(token);
result.add( x,token.toLowerCase());
//System.out.println("Key is" + x + "\t" + result.get(x));
x++;
}
}
line=br.readLine();
}
for(int w =0;w<x;w++){
c=0;
String copy=result.get(w);
int i;
for(i =0;i<stopWordsArray.length;i++){
if(copy.compareTo(stopWordsArray[i])==0){
c=1;
break;
}
}
if(c==0){
new_list.add(copy);
}
}
if(c==0){
Map<String, Integer> map = new HashMap<String, Integer>();
for (String temp : new_list) {
Integer count = map.get(temp);
map.put(temp, (count == null) ? 1 : count + 1);
}
int i=0;
int sort = 0;
String key1 = "";
for (Map.Entry<String, Integer> entry : map.entrySet()) {
sort = entry.getValue();
key1 = entry.getKey();
sorted.add(i,sort);
key.add(i,key1);
i++;
}
Integer maxi= Collections.max(sorted);
System.out.println(maxi);
Integer value = sorted.indexOf(maxi);
System.out.println(value);
System.out.println("Word is:" + key.get(value));
}
return ret; }
Here sorted is a list which contains frequencies of words and key is list which contains word.