Problem with using Java lists

Asked

Viewed 83 times

1

I have a file loaded with 300,000 prices. I need to put prices in ascending order in a list. If I put it in standard mode, analyzing in a linear way where to insert each price, my algorithm is very slow. Someone has an idea of a quick way to do this?

EDIT.:

These are random prices I need to sort.

19.5, 11.3, 17.43, 1.32, 36.45, etc...

I’m doing a context because what I really want to do is different a little but requires knowledge of my system. This is an abstraction.

EDIT. 2:

if (Type == 0) {
            for (int i = 0; i < Objeto.size(); i++) {
                //Ordena pelo menor preço. Se o primeiro elemento tiver preço menor que algum da lista completa,
                //tal elemento é inserido anterior a esse algum
                if (PrimeiroElemento.getPrice() > Objeto.get(i).getPrice()) {
                    return i;
                }
                if (PrimeiroElemento.getPrice() == Objeto.get(i).getPrice()) {
                    //se os preços forem iguais, ordena pelo tempo
                    if (PrimeiroElemento.getTimestamp() < Objeto.get(i).getTimestamp()) {
                        return i;
                    }
                }
            }
  • It would be interesting to add a small sample of how are these prices in the file by editing the question,

  • Done. I need basically an optimization method.

  • But how are they arranged in the file? In a single line like this, separated only by comma?

  • I already took them from the file and sent them to a list. Now I have a disorderly list with these values and I need to play for another list so that they are ordered. I thought of Collections Sort but every time I enter an element, I need the list sorted and ready to use. This creates the need to sort the list every time I enter a data, which is not feasible.

  • Example: I read from the disorderly list the values 17, 18 and 11. On the sorted list, I have to be 11, 17 and 18 so that anytime I need to use that sorted list. At the same time I am sorting I may need to use the data. So I am running the sort in a thread.

  • Instead of using a List at the time of capturing the data you can use the Set for example Set<Double> precos = new HashSet();

  • Set by default already keeps the data sorted in its insertion

  • You can post the code snippet that you fill the list?

  • Dude, that’s an abstraction. You will not understand my code very well because it is more complex, however I will post on an Dit in the question with an understandable part.

  • The best and most efficient way is to use the ConcurrentHashSet he is similar to Set however it is proper to work with multithreads. I recommend you to use it instead of the List, when you give the add on it it automatically already puts the item in the correct position of the list: Collection<Double> precos = new ConcurrentHashSet<>();

  • Ai when you are manipulating you can use in stream with the Java 8 in this way: precos.stream() you can perform any operation

  • Dude, I’m gonna take a look at this Currenthashset. Thanks.

  • I just found out that the ConcurrentHashSet has some ordering problems, I found the ConcurrentSkipListSet which is more stable, you can create it in the following way: Collection<Double> precos = new ConcurrentSkipListSet<>(Collections.reverseOrder());

  • I will post a code as an example of use

Show 9 more comments

1 answer

0

You can work with the ConcurrentSkipListSet, he work the same as Set however it is appropriate to work in multithreads and also more secure with respect to data order, follows an example

// Define a ordem
Comparator sort = Collections.reverseOrder();
// Cria a collection
Collection<Double> precos = new ConcurrentSkipListSet<>(sort);

// Teste crio um pool com 100 threads onde cada thread adiciona dentro
// da lista um valor aleatório
ExecutorService pool = Executors.newFixedThreadPool(100);
for (int i = 0; i < 300000; i++) {
    pool.submit(() -> {
        precos.add(Math.random());
    });

}

// Finaliza o pool
pool.shutdown();
System.out.println("Done!");

// Exibe o top 10
int index = 1;
for (double p : precos) {
    System.out.println(p);
    if (index++ == 10) {
        break;
    }
}

Note that at the end of the process the price Collection has all the data that the threads added randomly ordered.

The order by default is ascending (empty constructor) plus you can place decreasing order or customize your comparator passing on the builder of ConcurrentSkipListSet, example:

Comparator sort = new Comparator<Double>() {
    @Override
    public int compare(Double o1, Double o2) {
        return Double.compare(o1, o2);
    }
};

In this case you just need to adapt the Commander to work according to the specifications of the EDIT. 2: of your code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.