A
A
artshelom2016-11-17 13:18:56
Java
artshelom, 2016-11-17 13:18:56

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;
    }
}

The lookup method sorts the array by number, if the numbers are the same, then alphabetically
. The retriev method checks, if the word starts with the sent word, then it adds it to the list to be sent. If there was no sorting, then it starts it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-11-17
@TheKnight

TreeSet instead of ArrayList with custom comparator? But this is if there are no strings for the insertion order. For the multithreaded case, there are similar data structures.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question