The difference between a list in Java and a collection

Source: Internet
Author: User

Overall, List,set,map is the interface that implements the collection interface.

  1. Repeatability:
      • The list allows duplicate elements, which means that the list can insert new repeating elements without affecting the values of existing elements and their indexes.
      • Set does not allow duplicate elements, and the set collection does not allow the insertion of duplicate elements.
      • Map is a collection of mapping types that store elements in key-value pairs, where key (key) is unique and value (value) is repeatable.
  2. Null value:
      • The list allows any number of null values.
      • Set allows a maximum of one null value.
      • Map allows only one null key to appear, but allows any number of null values.
  3. Sorting:
      • The list maintains the order in which each element is inserted.
      • The elements in the set are unordered, but some of the set's implementation classes can sort the elements in some order. (Ps:linkedhashset are sorted by the insertion order of the elements.) )
      • Map, like set, wakes the elements out of order, but some of the map implementation classes also sort the elements. (Ps:treemap sorts the elements in ascending order by key, and Linkedhashmap sorts them according to the insertion sequence of each element)
  4. Implementation class:
      • List:arraylist, LinkedList and so on.
      • Set:hashset, Linkedhashset, TreeSet, SortedSet and so on.
      • Map:hashmap, TreeMap, Weakhashmap, Linkedmashmap, Identityhashmap and so on.
  5. Usage scenarios:
      • List (ArrayList) is a good choice if you need to always query based on the index value of an element.
      • If your data does not allow duplicate values to exist, set is the more appropriate choice.
      • If you need to preserve the insertion order of each element, then the list collection is preferred.
      • If your data needs to be key-value mapped, then use map.

Original reference: Java tutorial

The difference between a list in Java and a collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.