user :
I have a String
array with name and id, I need to convert that String
array to List
of objects.
This is my code:
private List<ObjectAttribute> getDtls(String newVal) {
ObjectAttribute object = new ObjectAttribute();
List<ObjectAttribute> objLst = new ArrayList<ObjectAttribute>();
String[] newImageVal = [step0005.jpg, 172B6846-0073-4E5B-B10A-DDD928994EA6, step0003.jpg, FBC8D143-2CD7-47E6-B323-31A0928A9338]
for (int i = 0; i <= newImageVal.length - 1; i++) {
object.setImageName(newImageVal[i]);
object.setImageId(newImageVal[++i]);
objLst.add(object);
}
return objLst;
}
but there is a problem that it always returns only the last value in objList
. Can anyone correct this code.
Sergey Benner :
or you may do something like this using the streams
way:
AtomicInteger ai = new AtomicInteger();
List<ObjectAttribute> objLst = Arrays.stream(newImageVal)
.map(img-> {
ObjectAttribute object = new ObjectAttribute();
object.setImageName(img);
object.setImageId(ai.getAndIncrement());
return obj;
}).collect(Collectors.toList())
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=327848&siteId=1