Rule of thumb for choosing an implementation of a Java Collection?

前端 未结 11 1515
执念已碎
执念已碎 2020-11-29 16:06

Anyone have a good rule of thumb for choosing between different implementations of Java Collection interfaces like List, Map, or Set?

For example, generally why or i

11条回答
  •  旧时难觅i
    2020-11-29 16:57

    Well, it depends on what you need. The general guidelines are:

    List is a collection where data is kept in order of insertion and each element got index.

    Set is a bag of elements without duplication (if you reinsert the same element, it won't be added). Data doesn't have the notion of order.

    Map You access and write your data elements by their key, which could be any possible object.

    Attribution: https://stackoverflow.com/a/21974362/2811258

    For more information about Java Collections, check out this article.

提交回复
热议问题