I'm using Java 8 and Groovy, unfortunately I can't use lambdas and I have two different lists of objects with one attribute in common, on the first List I get the most of data I need and on the second list I get the data in common, I wrote a for nested loop in order to update the first list with the attribute in common from the second list, I just want to know if there is a better way to improve this code:
List<Object1> firstList = dao.something();
List<Object2> secondList = dao.something2();
List<Object1> thirdList = new ArrayList<Object1>();
for (Object1 obj1 : firstList){
for(Object2 obj2 : secondList){
if(obj1.getSomething().equals(obj2.getSomething())){
obj1.setAttribute(ojb2.getAttribute);
thirdList.add(obj1);
}
}
}
Any help will be useful, thank you.
Map<ObjectN, Id>whereIdis the type of value returned bygetId().Map<IdType, Object2>. Then for each object from the first list, get id and get the desired object from the map if it is in the map.