Collection : A set of data that stores and manipulates elements that are not fixed.
The collection classes are located in the Java.util package.
a Java collection cannot hold a base data type, only a reference to an object.
in later versions of5.0,JAVA added automatic boxing , automatic unpacking,
If you want to deposit a basic type of data, the virtual machine converts the data into a wrapper class and then into the collection.
several common collections of Java:
Set,List , and Map
They're all in the java.util bag.
Set,List , and Map are all interfaces
Set the implementation class mainly has HashSet and the TreeSet
1 ImportJava.util.Iterator;2 ImportJava.util.TreeSet;3 4 Public classSettest {5 Public Static voidMain (string[] args) {6TreeSet TreeSet =NewTreeSet ();7Treeset.add ("Data 1");8Treeset.add ("Data 2");9Treeset.remove ("Data 1");//Remove an elementTenTreeset.clear ();//Clear One } A -}
List the implementation class mainly has ArrayList and the LinkedList
1 Importjava.util.ArrayList;2 3 Public classListtest {4 Public Static voidMain (string[] args) {5ArrayList ArrayList =NewArrayList ();6Arraylist.add ("Data 1");7Arraylist.add ("Data 1");8Arraylist.remove (0);//Remove from index9Arraylist.remove ("Data 1");TenArraylist.clear ();//Clear OneArraylist.get (0);//Get Value A } - -}
the implementation classes of MAP are mainly HashMap and TreeMap.
1 ImportJava.util.HashMap;2 ImportJava.util.Iterator;3 ImportJava.util.Set;4 5 Public classMaptest {6 Public Static voidMain (string[] args) {7HashMap HashMap =NewHashMap ();8Hashmap.put ("A", "1");9Hashmap.put ("B", "1");TenHashmap.remove ("a");//Remove by Key OneHashmap.clear ();//Clear AHashmap.get ("B");//get through Key -Set KeySet =Hashmap.keyset (); - //get all the keys theIterator Iterator =keyset.iterator (); - while(Iterator.hasnext ()) { - System.out.println (Iterator.next ()); - } + } - +}
objects in Set are not sorted in a particular way, and there are no duplicate objects. However, some of its implementation classes can sort the objects in the collection in a particular way, such as the TreeSet class, which can be sorted by default or by implementing java.util.comparator<type> interface to customize the sort method.
the objects in the list are sorted by index, and can have duplicate objects that allow objects to be retrieved at the indexed position in the collection, such as by List.get (i) to get a list The elements in the collection.
each element in the MAP contains a key object and a value object, which appear in pairs. Key objects cannot be duplicated, and value objects can be duplicated.
A collection of basic Java knowledge