I wonder if ArrayList<SimpleObject> and ArrayList<ComplexObject> has different performance effect if we are concerning on memory usage and navigating speed (foreach, getAt())? Why?
Thanks,
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your question isn’t very clear on what you are looking for, so I’ve made a few assumptions about what you are seeking.
If your concern is of the memory used by the list, then it will depend on the actual size of the objects being managed by the
ArrayList. IfSimpleObjects are lightweight compared toComplexObjectsthen the memory consumed on the heap will be higher in the latter case. However, the actual memory consumed by the array lists will be more or less constant for arraylists of equal number of objects, as the lists only contain references to the actual objects on the heap.On the topic of runtime performance, this depends on the method being invoked. Methods like
get(int index)andadd(E element)will always have the same runtime performance characteristics irrespective of the type being used. This is due to the nature of the method: their behavior does not depend on the type of the Object present in the list.On the other hand, the performance of
indexOf(Object object)will depend on how theequalsmethod is implemented. For trivial cases, it would easy to infer thatindexOfwill run faster forArrayList<SimpleObject>than forArrayList<ComplexObject>, assuming that theequals()implementation ofSimpleObjectwill run faster than that ofComplexObject.If your concern is of memory consumption during execution of the
ArrayListmethods, then it is not bound to be different as most of the methods work off references of objects. There are exceptions liketoArray()which would require less memory forSimpleObjectthan forComplexObject.