SuF
Legend
- Joined
- Jan 21, 2007
- Sythe Gold
- 1,234
- Messages
- 14,211
I made this method and it works but is slow (I've provided a full test class for ya). With a million Strings stored, it takes 229 seconds to find the 10,000th place highest number. If you reduce the number of stored values to like 100k, it works much faster but I still want to streamline it anyways. Thoughts?
Code:
import java.util.HashMap;
public class MethodTest
{
private HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
public MethodTest()
{
for (int a = 0; a < 1000000; a++)
{
wordCount.put(Integer.toString(a), a);
}
}
public String getTopWord(int index)
{
String[] list = new String[index];
for (int a = 0; a < list.length; a++)
{
list[a] = null;
}
for (String word : wordCount.keySet())
{
for (int a = 0; a < list.length; a++)
{
if (list[a] == null || wordCount.get(word) > wordCount.get(list[a]))
{
for (int b = list.length - 1; b > a; b--)
{
list[b] = list[b - 1];
}
list[a] = word;
break;
}
}
}
return list[index - 1];
}
public static void main(String[] args)
{
long a = System.currentTimeMillis();
System.out.println(new MethodTest().getTopWord(10000));
System.out.println((System.currentTimeMillis() - a));
System.out.println((System.currentTimeMillis() - a) / 1000);
}
}

