Today at work I had a discussion with 2 developers regarding the way to add an element into a list. They told me that this is the more efficient way:
final FileInfo fileInfo = new FileInfo();
for (GroupInfo groupInfo : cft.getGroupFiles()) {
fileInfo.setClassName(groupInfo.getClassGeneration());
fileInfo.setFileId(groupInfo.getId().getCfgFileTypeId());
fileInfoList.add(fileInfo);
}
But I'd always done it in a slightly different way:
for (GroupInfo groupInfo : cft.getGroupFiles()) {
FileInfo fileInfo = new FileInfo();
fileInfo.setClassName(groupInfo.getClassGeneration());
fileInfo.setFileId(groupInfo.getId().getCfgFileTypeId());
fileInfoList.add(fileInfo);
}
So, what is the most efficient way to do this? Why? I think that the first way uses less memory than the second way, but the second way generates a new instance and for an instance, a new memory-reference and then adds it to the list, but I had bad experiences using the first way when I had to convert or use some elements from the list using the first way...So, can you help me with this? Thanks!