Reputation: 4919
I have a List of Images
List<Image> images = getImages();
Image.java
public class Image {
private String imageId;
// getter and setter methods
// ...
}
And I have a List<String> imageIds
to replace the imageId
in each Image
object. What would be the best way to do it?
Upvotes: 2
Views: 167
Reputation: 4223
You question sounds strange. It's not very clear, but if you need to add/remove/replace elements in the list, you should use ListIterator. It has a set method, which allows to replace elements.
Upvotes: 0
Reputation: 35547
You have mention in the comment both List has same order, So you can try this.
List<String> imageIds=new ArrayList<>();
List<Image> images = new ArrayList<>();
for(Image img:images){
img.setImageId(imageIds.iterator().next());
}
Upvotes: 0
Reputation: 68905
Iterate over the Images list and call setImageId(imageId)
on it.
Iterator<String> imageIDs = imageIds.iterator();
for(Image image : images){
image.setImageId(imageIDs.next());
}
Since both are List I am assuming order is important as it is inserted.
Upvotes: 0
Reputation: 21
I think traversing the List<Image>
and for each element call the imageID's setter will just works fine.
Even if there is a way to do it with less code, it probably uses traversing fundementally.
Upvotes: 1