In actual programming, we often need to temporarily store data to implement a specific function. In pure C language, we need to implement a data structure (such as a linked list or Queue) to access elements, which is not only tedious, and the efficiency is not high (the encapsulation of the data structure by the class library is often optimized and has good performance ). In Java, we can use an object container (container) to conveniently access data.
1. List Interface)
The list interface is a sub-interface of the Java. util. collction interface. It adds an index-based object retrieval method based on the collection interface. Therefore, the list structure is specific. Each element in the list is added in order, and the index access element can be specified, similar to an array.
Arraylist is a class that implements the list interface. arraylist uses an array structure to implement the list data structure. Therefore, for operations that require frequent and Random Access to objects, arraylist can achieve better efficiency. However, if you delete and add objects, the efficiency will be very low, because arraylist needs to move a large number of elements inside it. The following is an example of using arraylist:
Package CLS; import Java. util. *;/*** arraylist array test * 2013.3.22 **/public class arraylisttest {public static void main (string [] ARGs) {arraylist <string> List = new arraylist <string> (); scanned scan = new partition (system. in); While (true) {string input = scan. next (); If (input. equals ("Q") break; List. add (input); // use the add () method to add elements} // use the iterator to traverse the output iterator it = List. iterator (); While (it. hasnext () system. out. println (it. next ());}}
The listlist class implements the list interface using a linked list. This means that the shortlist can be effectively added, deleted, or inserted. However, for access operations, it is not as efficient as arraylist.
We can use the queue list to implement data structures such as stack (strack) and queue (queue. For example:
Package CLS; import Java. util. *;/*** use consumer list to implement stack * 2013.3.3.22 **/public class Consumer liststack {consumer list <string> list; public consumer liststack () {This. list = new challenge list <string> ();} public void push (string Str) // pressure stack {list. addfirst (STR);} Public String top () {return list. getfirst ();} Public String POP () // output stack {return list. removefirst ();} public Boolean isempty () {return list. isempty ();} p Ublic static void main (string [] ARGs) {consumer liststack stack = new consumer liststack (); consumer SC = new consumer (system. in); string STR; while (true) {STR = SC. next (); If (Str. equals ("Q") break; stack. push (STR); // press the stack} while (! Stack. isempty () system. Out. println (stack. Pop ());}}
Package CLS; import Java. util. *;/*** use the queue list to implement the queue * 2013.3.22 **/public class implements listqueue {public static void main (string [] ARGs) {shortlist <string> q = new shortlist <string> (); shortsc = new shortlist (system. in); string STR; while (true) {STR = SC. next (); If (Str. equals ("Q") break; // offer () queues Q. offer (STR) ;}// poll (); returns NULL while (STR = Q. poll ())! = NULL) {system. Out. println (STR );}}}
2. Set Interface
The Set interface is also a sub-interface of collection. The objects in the list container can be repeated, but the objects in the Set container must be unique.
The hashset class implements the set interface. Hashset uses the hash method to insert and sort elements, so that it can be efficiently searched and inserted. For example:
Package CLS; import Java. util. *;/*** use hashset for quick search and insert operations * 2013.3.22 **/public class hashsettest {public static void main (string [] ARGs) {hashset <string> set = new hashset <string> (); then SC = new then (system. in); string STR; while (true) // Insert the element {STR = SC. next (); If (Str. equals ("quit") break; set. add (STR);} while (true) // query element {STR = SC. next (); If (Str. equals ("quit") break; // call the contains () method to find whether the element if (set. contains (STR) system. out. println ("yes"); else system. out. println ("no ");}}}
3. sortedset Interface
Sortedset is an interface that implements the sorting function. classes that implement this interface are ordered.
Treeset is the only class in Java SE that implements the sortedset interface. It uses the red-black tree structure to sort inserted elements. For example, output the input string in Lexicographic Order:
Package CLS; import Java. util. *;/*** use the treeset class for sorting * 2013.3.3.22 **/public class treesettest {public static void main (string [] ARGs) {/*** use the default treeset sorting method **/treeset <string> set = new treeset <string> (); set. add ("Zoo"); set. add ("bird"); set. add ("Pear"); set. add ("iPod"); // use enhanced for loop to display the sorted object for (string STR: Set) system. out. println (STR); system. out. println ("------------------------------------");/*** sort by custom method **/set <string> S = new treeset <string> (New treesetcustom (); S. add ("Zoo"); S. add ("bird"); S. add ("Pear"); S. add ("iPod"); For (string STR: S) system. out. println (STR) ;}}/*** custom sorting rules ** 2013.3.3.22 **/class treesetcustom implements comparator {public int compare (Object O1, object O2) {// return (string) O1 in the Lexicographic Order ). compareto (string) O2) * (-1 );}}
We can also customize sorting rules to meet the actual needs in practical applications. To define your own sorting rules, you must define an object that implements the java. util. comparator interface and implement the comare () method in this interface. Example above
4. Map Interface
Objects that implement the java. util. map interface map keys to values ). When saving an object to map, you must specify a key for retrieving the object. Each key in map is unique and must not be repeated.
Hashmap implements the map interface. Hashmap uses the hash method internally, which improves the search efficiency. For example:
Package CLS; import Java. util. *;/*** use the hashmap class to implement the key-value Storage Structure * 2013.3.22 **/public class hashmaptest {public static void main (string [] ARGs) {hashmap <string, string> set = new hashmap <string, string> (); // put ([Key], [value]); set. put ("animal", "cat"); set. put ("food", "hotdog"); system. out. println (set. get ("animal"); system. out. println (set. get ("food "));}}
Treemap also implements the map interface, which uses the red/black tree to sort inserted objects. Its operations are similar to those of hashmap ..
PS: After learning this chapter, I feel like a bunker !!