Hello, here is the essence of my problem, there are several collections:
private List<Order> fstBookBUY; private List<Order> fstBookSELL; private List<Order> sndBookBUY; private List<Order> sndBookSELL; private List<Order> trdBookBUY; private List<Order> trdBookSELL; fstBookBUY , fstBookSELL is one subject
sndBookBUY , sndBookSELL is the second subject
trdBookBUY , trdBookSELL is the third subject
Lists of 2 for each subject, it is because in one list, his offers to buy, in the other about the sale (All sorted by price ).
Each collection is filled with class objects:
class Order { private String book; private String operation; private int volume; private float price; private int orderId; Order(String book, String operation, int volume, float price, int orderId) { this.book = book; this.operation = operation; this.volume = volume; this.price = price; this.orderId = orderId; } // гетеры сетеры } We need an algorithm that checks that the two Order objects have the same price field, and the book and operation different, and if so, perform the operation automatically.
Well, for example: there are two Order which
1st order - price = 10 , operation = "SELL" , volume = 100 , book = "book-1" ;
2nd order - price = 10 , operation = "BUY" , volume = 130 , book = "book-2" ;
Then the first order disappears altogether, and the second volume becomes 30
Or if the volume of both was the same, and they both disappear.
Or if the first order volume was more than 20, the second one disappears, and the first one becomes 20.
The main thing is that the price same and orders with different book and operation . How many could have bought / sold that I could not, that part of the request remained.
Speed matters. I would be grateful for the code, and for just thoughts or ideas about how this could be done. This task is my final for the collections, so apparently it is necessary, based on the properties of different collections, to come up with some trick to speed up the work.
price(and the second sorting field should gobook- if you can “cut” only differentbook) - and then this is a normal linear merge. compared the first two, either an operation, or - left the smaller one as it is, shifted its list. compared again - and so on until some list is exhausted, after that the second one is simply copied. O (n). Faster, I think, will not work. - Harry