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,
– user28595
Done. I need basically an optimization method.
– Marcos
But how are they arranged in the file? In a single line like this, separated only by comma?
– user28595
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.
– Marcos
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.
– Marcos
Instead of using a
List
at the time of capturing the data you can use theSet
for exampleSet<Double> precos = new HashSet();
– brow-joe
Set by default already keeps the data sorted in its insertion
– brow-joe
You can post the code snippet that you fill the list?
– brow-joe
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.
– Marcos
The best and most efficient way is to use the
ConcurrentHashSet
he is similar toSet
however it is proper to work with multithreads. I recommend you to use it instead of theList
, when you give theadd
on it it automatically already puts the item in the correct position of the list:Collection<Double> precos = new ConcurrentHashSet<>();
– brow-joe
Ai when you are manipulating you can use in
stream
with theJava 8
in this way:precos.stream()
you can perform any operation– brow-joe
Dude, I’m gonna take a look at this Currenthashset. Thanks.
– Marcos
I just found out that the
ConcurrentHashSet
has some ordering problems, I found theConcurrentSkipListSet
which is more stable, you can create it in the following way:Collection<Double> precos = new ConcurrentSkipListSet<>(Collections.reverseOrder());
– brow-joe
I will post a code as an example of use
– brow-joe