0

According to the following code, I have to set the value of objects from one list by getting the values from other objects. For the reason of reducing process, I'm deleting each object from the second list after setting value. Doing so, the second list will reduce for each loop as well as the CPU process. Is my thought right? Will I get to reduce some process doing so?

for(Product product : _session.getProducts()){

    for(Product newProduct : _newSession.getProducts()){

        if(product.getID() == newProduct.getID()){

            product.setValue(newProduct.getValue());

            _newSession.getProducts().remove(newProduct);

            break;
        }

    }

}
Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
  • 2
    I don't think you are going to get an performance gain doing what you are doing but you will get a ConcurrentModificationException if you try and remove an item from a collection while you iterate over that collection. – bhspencer May 27 '15 at 20:47
  • It would make more sense to sort your lists by ID and then iterate on them together rather than in nested loops. – RealSkeptic May 27 '15 at 20:48
  • What does `_newSession.getProducts()` *do*? Without knowing that, we can't possibly know what the effect will be. It does sound like you could do with creating a `Map` (either of the new session or the existing one) or something similar, so you could then just iterate once and find matches. – Jon Skeet May 27 '15 at 20:49
  • @bhspencer I did all the tests with that and I didn't get any `Exception` though. In theory, there is a little gain of performance, but I'd like to know in practice. – Lennon Spirlandelli May 27 '15 at 21:05
  • @RealSkeptic If I sort the list it'll waste some time doing so. And that code is just using an `if`, so I think that sorting my list wouldn't be needed in that case. – Lennon Spirlandelli May 27 '15 at 21:09
  • @JonSkeet `_newSession.getProducts()` gives me a list of product coming from an `API` and `_session.getProducts()` is a list that I have in cache. Sometime the list from `API` could come with the objects in different positions. – Lennon Spirlandelli May 27 '15 at 21:14
  • @Lennon currently your time is O(n²) (assuming the lists are the same length), while sorting would be O(n log n). Removing does *not* save anything because the removal operation is not O(1) and even if it was, it doesn't lower the order of magnitude. And it will throw an exception, as you were told. – RealSkeptic May 27 '15 at 21:37
  • Or Jon's method is even quicker: scan the lists each linearly, creating maps from ID to object. Then scan your head list and get the matching object directly from the map. That will be O(n). – RealSkeptic May 27 '15 at 21:42
  • @RealSkeptic That code is just an example. In my real code, the lists don't have the same size, I need to compare three attributes in the `if` and I have to set more attributes than only one; Nevertheless, I must get the objects of the `_newSession.getProducts()` that are left and add into `_session.getProducts()` – Lennon Spirlandelli May 28 '15 at 13:39
  • Even if it's not the same length, set map method will work properly and still be more efficient than your method. Perhaps you should describe your full problem properly in your question - which values you need to compare, which lists you have, which attributes you need to set. But in any case - removing from a list is not going to improve the code you gave, except under very special conditions. – RealSkeptic May 28 '15 at 14:48

1 Answers1

0

I don't think you can measure the actual CPU process saved. In theory might be some savings, perhaps if the list are huge. you should look for alternative solutions. One would be to use Set instead of lists, an properly implement equals and hashCode for Product class. The search algorithms for Set implementations are very fast: O(log n). And is very simple to implement the equals since you have only one atrribute that differentiate the objects, the ID.

iullianr
  • 1,274
  • 10
  • 11
  • It's not only one attribute that differ the objects in my case. That code is just an example. I have 3 different things to compare. And the list isn't huge. – Lennon Spirlandelli May 27 '15 at 21:18