Collection: Only objects can be stored, the object types can be different and the length can be variable.
Commonly used interfaces and classes:
1. List interface (orderly and repeatable): ArrayList class, LinkedList, Vector class
2. Set interface (unordered, cannot be repeated): HashSet class, TreeSet class
3. Map interface (key-value pairs, key-only, value not unique): HashMap class, Hashtable class, TreeMap class
Loop traversal of collection classes
1. Ordinary for loop: such as for(int i=0;i<arr.size();i++) {…}
2. foreach (enhanced for loop): such as for (Object i: arr) {…}
3. Iterator (iterator): For example, Iterator it = arr.iterator(); while(it.hasNext()){ Object o =it.next(); …}
Note: Arrays or collections cannot be modified during traversal, while for loops can be modified during traversal.
The difference between ArrayList, LinkedList and Vector
ArrayList: High efficiency, mostly used for querying
LinkedList: mostly used for insertion and deletion
Vector: thread-safe, mostly used for querying
Code:
import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Vector;public class ListTest {public static void main(String[] args){List arrayList=new ArrayList();List linkedList=new LinkedList();List vector=new Vector();arrayList.add("1"); //character type arrayList.add("1"); //Repeat element arrayList.add("2");arrayList.add(1); //Number type linkedList.add("1");linkedList.add("1");linkedList.add("2");linkedList.add(1);vector.add("1");vector.add("1");vector.add("2");vector.add(1);for(Object obj:arrayList){ //Foreach loop System.out.println(obj);}for(int i=0;i<linkedList.size();i++){ //Ordinary for loop System.out.println(arrayList.get(i));}Iterator it = vector.iterator(); //Iterator while(it.hasNext()){Object j=it.next();System.out.println(j);}}}The difference between HashSet and TreeSet
HashSet: HashSet is implemented by a hash table. The data in the HashSet is unordered. You can put null, but you can only put one null.
TreeSet: TreeSet is implemented by a two-difference tree. The data in Treeset is automatically sorted and null values are not allowed to be placed.
Code:
public class SetTest {public static void main(String[] args){Set hashSet=new HashSet();Set treeSet=new TreeSet();hashSet.add("1"); //character type hashSet.add("1"); //repeat element hashSet.add("2"); hashSet.add(1); //number type treeSet.add("1"); treeSet.add("1"); treeSet.add("2"); //treeSet.add(1); //report error, treeSet cannot add different data types for(Object i:hashSet){ //Foreach loop System.out.println(i);}Iterator it = treeSet.iterator(); //Iterator while(it.hasNext()){Object j=it.next();System.out.println(j);}}}Note: The Set interface does not have a get method, so you cannot use a normal for loop to traverse.
The difference between HashMap, Hashtable and TreeMap
HashMap: HashMap allows one null key and multiple null values
Hashtable: The key and value of hashtable are not allowed to be null
TreeMap: It can sort the records it saves according to the key. By default, it is sorted in ascending order of key values. You can also specify the sorted comparator. When it is traversed with Iterator, the records obtained are sorted.
Code:
import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.TreeMap;public class MapTest {public static void main(String[] args){Map hashMap=new HashMap();Map hashtable=new Hashtable();Map treeMap=new TreeMap();hashMap.put(1, "1"); //character type value hashMap.put(2, "1"); //The same value hashMap.put(3, "2");hashMap.put(4, 1); //The numeric type value hashMap.put("5", 1); //The character type key hashtable.put(1, "1"); hashtable.put(2, "1");hashtable.put(3, "2");hashtable.put(4, 1);hashtable.put("5", 1);treeMap.put(1, "1");treeMap.put(2, "1");treeMap.put(5, "2");treeMap.put(4, 1);//treeMap.put("5", 1);treeMap.put(1, "1");treeMap.put(2, "1");treeMap.put(5, "2");treeMap.put(4, 1);//treeMap.put("5", 1); //An error is reported, TreeMap cannot add different types of keys //Transip hashMap key for(Object key:hashMap.keySet()){System.out.println(key);}//Transip hashtable value for(Object value:hashtable.values()){System.out.println(value);}//Transip hashMap key value pair Set set = hashMap.keySet(); for(Iterator iter = set.iterator(); iter.hasNext();){Object key = iter.next();Object value = hashMap.get(key);System.out.println(key+"/t"+value);}//Iteratively traverse hashtable key-value pairs, reverse order! Iterator table = hashtable.entrySet().iterator(); while(table.hasNext()){Map.Entry entry = (Map.Entry) table.next(); Object key = entry.getKey(); //Get key Object value = entry.getValue(); //Get the value System.out.println(key+"/t"+value); }//Iteratively traverse treeMap key-value pair Iterator tmp = treeMap.entrySet().iterator(); while(tmp.hasNext()){Map.Entry entry = (Map.Entry) tmp.next(); Object key = entry.getKey(); //Get key Object value = entry.getValue(); //Get the value System.out.println(key+"/t"+value); }}}Classes about thread safety include: Vecto, HashTabl, StringBuffe
Non-thread safety: ArrayList, LinkedList, HashMap, HashSet, TreeMap, TreeSet, StringBulider
Note: ConcurrentHashMap can be used instead of HashMap for thread safety and is more efficient than Hashtable.
Java's own mechanism cannot fully guarantee thread safety. You need to manually code and control it yourself.