Answer the question
In order to leave comments, you need to log in
How to speed up processing?
Hi all.
Need a little help. How can sorting be optimized?
Creates an object of this class and adds a list to it like this: "word number"
When accessed, sorts it. Is there any way to optimize the process?
public class Spisoc{
private List<String> str = new ArrayList<>();
private List<Integer> number = new ArrayList<>();
private ArrayList<String> list;
public synchronized void add(String text){
str.add(text.split(" ")[0]);
number.add(Integer.valueOf(text.split(" ")[1]));
}
private void lookup(){
int sdr = 0;
list = new ArrayList<>();
ArrayList<Integer> nomStr = new ArrayList<>();
nomStr.addAll(number);
TreeSet<String> stroka = new TreeSet<>();
Collections.sort(nomStr);
for (int er = 0; er < number.size(); er++){
stroka.add(str.get(number.indexOf(nomStr.get(er))));
number.set(number.indexOf(nomStr.get(er)), 0);
if (er != 0)
sdr= nomStr.get(er - 1);
if (nomStr.get(er) != sdr || er==0) {
for (String gt : stroka)
list.add(gt);
stroka.clear();
}
}
}
public String retriev(String text) {
if (str.size() == 0)
return null;
if (list==null)
lookup();
String rezult = "";
for (int er = list.size() - 1, sy = 0; er >= 0 && sy <= 9; er--){
if (list.get(er).startsWith(text)) {
sy++;
rezult += list.get(er) + "\n";
}
}
return rezult;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question