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