The most lightweight Java collection

If I want to create a Java collection and just want to fill it with elements and traverse it (I don't know the necessary size in advance), that is, all I need is the collection < E > Add (E) and collection < E > Iterator (), which concrete class should I choose? For example, is there any advantage in using set instead of list? Which one costs the least?

Solution

I may just use ArrayList or LinkedList Both support the add and iterator methods, and the more direct of them has any significant overhead

No, I wouldn't say that (unless you depend on the order of elements, in which case you must use list, or you want to prohibit repetition, in which case you should use set.)

(I don't see how any set implementation beats the list implementation of the add / iterator method, so I may use list even if I don't care about order.)

This sounds like a micro benchmark, but if I'm forced to guess, I'll say ArrayList (or LinkedList when ArrayLists need to reallocate memory often)

The above is all the content of the lightest Java collection collected by programming house for you. I hope this article can help you solve the program development problems encountered by the lightest Java collection.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>