The difference between list and set in the Java collection

Source: Internet
Author: User

The difference between list and set in the Java collection.
The list can store elements as ordered and the elements can be the same.
The set storage element is unordered and the element cannot be the same.
Here are a few pieces of code to feel:



  New ArrayList (); // constructs a list object    List.add (1);    List.add ("string");    List.add (true);    List.add (3.14);    List.add (null);      for (int i = 0;     < size ();    I+ +)            System.out.println (List.get (i));        }


Output results: 1
String
True
3.14
Null


Here's about set.
New HashSet ();  // the data in the hash table is irregular, and hashset is one of the implementation set classes, which can be viewed in the API documentation        List.add (1);        List.add (1);        List.add ("string");        List.add (true);        List.add (3.14);        List.add (null);                              = list.iterator ();          while (It.hasnext ()) {// whether there is the next element             System.out.println (It.next ());        }


The output is: null
1
String
3.14
True
It can be seen that the second code output rule is not regular, and the number 1 appears only once.

The difference between list and set in the Java 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.