In the process of programming, the storage and application of multi-data is a more troublesome thing, before I just know to use arrays and their own write encapsulation class to solve, but these two methods do not apply in some functions, For example, we have to get a drop-down menu based on the data from one of the tables in the database, and we need to get the content displayed to the user and the corresponding ID from the database. in this case, because such a pair of data in the database generally have several, so the encapsulation class does not apply, and the array can only store a single data type, and only through the cursor to get the corresponding value, and the most troublesome is that the array at the time of initialization must initialize the length, So also the array is not very suitable, this time we can use the Java official supply of the container class ( Collection Class ).
The container class ( Collection Class ) is the kind of storage that Java has officially provided to us, which extends the freedom of use of arrays, such as variable lengths, data that can store multiple data types, and so on. Container classes in the Java system is not just a class, but a lot of container classes constitute a framework, system, different container classes have different storage methods, because there are too many container classes, so Java official set up a series of interfaces, all the container classes have to be implemented.
Commonly used container classes can be broadly divided into three broad categories:List, Set, Map
Let's start by introducing List:
list is in order, which is commonly used mainly Vector, ArrayList, LinkedList three container class ,List of the main use of the following methods:
ArrayList ls=new ArrayList (); More information is defined as List ls=new ArrayList (); Question: Why do you define this? Under certain circumstances, it may be necessary to switch the implementation class//linkedlist ls=new LinkedList ();//change too large if, below someone calls to the List interface there is no method//list ls=new LinkedList (); /No matter what the implementation class, no problem, because the interface has not changed//----------------------------------------------------------------------------------- List can hold any type of data//In fact, all classes are subclasses of the object class, when deposited, all types are broken into object//So, when taken out, definitely need to convert STR ing a= "a"; int i=10; Double d=1000.0; Ls.add (a); Ls.add (i); Ls.add (d); Element string a2= (String) ls.get (0) can be accessed sequentially, depending on the element's index/cursor; int i2= (int) ls.get (1),//int is the basic data type, not the encapsulation class, cannot convert int i2= (Integer) ls.get (1) directly; Double d2= (Double) ls.get (2); If it is more than jdk1.5, you can use generics//generics: Limit only one type of list<string> ls2=new arraylist<string> () is stored; Ls2.add ("a"); Ls2.add ("B"); Ls2.add ("C"); String s=ls2.get (0);//No re-conversion//-------------------------------------------------------------------- ---------------//You receive a list from someone else, how to take out all the data for (int j=0;j<ls2.size (); j + +) {Sy Stem.out.println (Ls2.get (j)); }//or convert into iterators to traverse out Iterator it=ls2.iterator (); while (It.hasnext ()) {System.out.println (It.next ()); }
In the three common list container classes mentioned above, bothArrayList and vectors use arrays to store data, both of which are implemented by using arrays, so index data is fast , but because inserting data involves memory operations such as array element movement , so inserting data is slow , and because the methods in the Vector are thread-safe, That is, all methods have added synchronous lock synchronized, so the performance is worse than the ArrayList .
linkedlist is stored using a doubly linked list, which requires a forward or backward traversal of the index data, but only the front and back items of the item are recorded when the data is inserted, so its index data is slow to insert data quickly .
set is unordered, which can not contain duplicate elements , which are commonly used mainly:HashSet, TreeSet,set The main use of the following methods:
Set hs=new HashSet ();//cannot insert duplicate data //list HS = new ArrayList ();//can insert duplicate data Student s=new Student (1, "Zhangsan"); Hs.add (s); S.name= "edit"; Boolean isadded = Hs.add (s); Duplicate data, can no longer insert System.out.println (isadded),//false Hs.add (New Student (2, "Lisi")); Hs.add (New Student (3, "Wangwu")); Hs.add (New Student (1, "Zhangsan"));//This is not duplicate data, is a new object //----------------------------------------------------- ------------------------------ //No order, only iterative values, the order will not be the case. Iterator it=hs.iterator (); while (It.hasnext ()) { System.out.println (It.next ());
HashSet are unordered, and TreeSet are sequential, but the order of TreeSet is different from the List as in order of insertion, It is ordered according to the collation of the element object's own definition, so the former allows for null and is more efficient, and the latter is not allowed to put null, and is less efficient.
map is a kind of container class of storing data in the way of key-value pair , which mainly include:HashMap, HashTable, TreeMap, the main use ofmap is as follows:
HashMap hm=new HashMap (); Hashmap<string,string> hm=new hashmap<string,string> (); String a= "a"; int i=10; Double d=1000.0; Put (key, value) Hm.put ("ZFC", a); Hm.put ("ZX", I); Hm.put (3,D); Hm.put (3,12.0); Key, overwriting the existing value string a2= (String) hm.get ("ZFC"); int i2= (Integer) hm.get ("ZX"); Double d2= (Double) hm.get (3); System.out.println (a2+ "" +i2+ "" +d2 "); Traverse Mode 1: Suggested way, high efficiency some Iterator Iterator = Hm.keyset (). Iterator ();//iterator iterators, press key to iterate through while (Iterator.hasnext ()) {System.out.println (Hm.get (Iterator.next ())); }//Traversal mode 2 Iterator it = Hm.entryset (). Iterator ();//iterator iterator, press entry to iterate through while (It.hasnext () ) {Java.util.Map.Entry Entry = (java.util.Map.Entry) it.next (); Entry.getkey () returns the key//Entry.getvalue () corresponding to this key, returning the value corresponding to this key System.out. println (Entry.getvalue ()); }
HASHMAP allows null (NULL) and Null values (NULL), non-thread safe, and high efficiency.
Hashtable does not allow null keys (NULL) with null values (NULL), thread safety, and low efficiency.
TreeMap can sort their saved records according to key (similar to TreeSet), by default sort by key, but you can also specify a sort comparer, when iterating through iterators, the resulting records are sorted, TreeMap does not allow a null key (NULL) but can have a null value (NULL).
java--Container Class (Collection Class)