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; 

And there is a method that fills them with data distributing from the input collection:

  private void segregateBooks(List<Order> orders) { for (Order order : orders) { if ("book-1".equals(order.getBook())) { if ("BUY".equals(order.getOperation())) { fstBookBUY.add(order); } else { fstBookSELL.add(order); } } if ("book-2".equals(order.getBook())) { if ("BUY".equals(order.getOperation())) { sndBookBUY.add(order); } else { sndBookSELL.add(order); } } if ("book-3".equals(order.getBook())) { if ("BUY".equals(order.getOperation())) { trdBookBUY.add(order); } else { trdBookSELL.add(order); } } } } 

I want to speed up this method, if possible. Maybe there is some tricky way?

    2 answers 2

    It is difficult to advise anything without understanding the ultimate goal. To the question “What kind of hammer it is easier to hammer in the screws”, I do not want to advise the model of the hammer, but hint at the screwdriver.

    If in the end you need to make selections by the name of the book and the type of order, you can use this method.

     private Map<String, Order> books = new HashMap<>(); private void segregateBooks(List<Order> orders) { for (Order order : orders) { books.put(order.getBook() + order.getOperation(), order); } } 

    If you need access to the lists by attributes, then you simply make the nested construction

     Map<String, Map<String, Order>> books = new HashMap<String, Map<String, Order>>(); private void segregateBooks(List<Order> orders) { for (Order order : orders) { Map<String, Order> inner = books.get(order.getBook()); if(inner == null){ inner = new HashMap<String, Order>(); books.put(order.getBook(), inner); } inner.put(order.getOperation(), order) } } 

    etc.

    • I feel that your version of Pts is good but I do not understand how it works. Here the inner object is a temporary cycle, and it will disappear. If it is not equal to zero, then you write an operation and an order into it. But why? What is next, it will be erased ... - Pavel
    • one
      @Pavel, why should he disappear? He fits with the books and stays there. With HashMap, the put () method works with a value, not a reference. And get () is the opposite. - rjhdby
    • one
      @Pavel, however, in Map books it will remain. Perhaps this code is more intuitive: books.putIfAbsent(order.getBook(), new HashMap<>()); Map<String, Order> inner = books.get(order.getBook()); inner.put(order.getOperation(), order); books.putIfAbsent(order.getBook(), new HashMap<>()); Map<String, Order> inner = books.get(order.getBook()); inner.put(order.getOperation(), order); which does the same thing. - Regent
    • one
      @Pavel is very simple. First, we assign an inner object reference to the object we’re retrieving books.get(order.getBook()) . If it is null, then we instantiate a new HashMap and put it in books . In the last line, inner and the object in books is the same object. At the next iteration of the loop, we have a new local inner , since we will re-define it Map<String, Order> inner = ... - rjhdby
    • one
      @Paul do so for example Map<String, Map<String, List<Order>>> . In general, such problems should always be considered from the point of view of a compromise between simplicity of analysis and simplicity of work with the result. In your case, I wouldn’t steal at all on parsing the source sheet, but would just do the getBuyOrders(String bookId) and getSellOrders(String bookId) in which I loop through the list and return the right one - rjhdby

    Use multithreading. On Java8:

     private void segregateBooks(List<Order> orders) { orders.parallelStream().forEach(order -> { if ("book-1".equals(order.getBook())) { if ("BUY".equals(order.getOperation())) { fstBookBUY.add(order); } else { fstBookSELL.add(order); } } if ("book-2".equals(order.getBook())) { if ("BUY".equals(order.getOperation())) { sndBookBUY.add(order); } else { sndBookSELL.add(order); } } if ("book-3".equals(order.getBook())) { if ("BUY".equals(order.getOperation())) { trdBookBUY.add(order); } else { trdBookSELL.add(order); } } }) }