I am somewhat new to working on Java 8 and refactoring old code with (which seems to be a good use case) for the thread. The older code "works", but for my eyes it looks really inefficient.
The short version of my question is that I am trying to find one element from the list and replace it with an updated version of the same element (the key is the same, but the properties have different values ββeach time the code is called).
try { List<Object> items = lookup(itemCache.getKey()); for (int i = 0; i < items.size(); i++) { Object originalObject = items.get(i); if (originalObject.getPropValue() == newObject.getPropValue()) { List<Object> newItems = new ArrayList<>(items); newItems.set(i, newObject); putIntoCache(newObject.getKey(), newItems); break; } } } catch (Exception ex) { }
Based on what I've read about threads so far, it seems that I need to use .map() or .filter() to highlight the element that I want to identify, but it also looks like the operations that occur after filter or map in the stream operator will work either not in the full list or in the list where .map() affects each element.
It seems simple, but I'm struggling to circle my head. Since the initial search is a List , I thought a thread could replace all of this. ArrayList<>() appears in the source code, but the order of the elements is not important, as long as I can replace this element with its key.
If you decide to help, thanks.
source share