Java from getting started to mastering 09-Collections

Source: Internet
Author: User

The Java collection classes have the following main types:

1, list structure of the collection class
ArrayList class, LinkedList class, Vector class, Stack class;

Importjava.util.ArrayList; Public classTest { Public Static voidMain (String args[]) {clerkmanage cm=NewClerkmanage (); Clerk Clerk1=NewClerk ("01", "Song Jiang", 40, 20000); Clerk Clerk2=NewClerk ("02", "Wu use", 38, 18000); Clerk Clerk3=NewClerk ("03", "Lin", 42, 15000);        Cm.addclerk (CLERK1);        Cm.addclerk (CLERK2);        Cm.addclerk (CLERK3); Cm.showinfo ("02"); Cm.updatesalary ("01", 25000); Cm.removeclerk ("03"); }}classClerkmanage {PrivateArrayList Al =NULL;  PublicClerkmanage () {Al=NewArrayList (); }     Public voidAddclerk (Clerk clerk) {Al.add (Clerk); }     Public voidShowinfo (String no) { for(inti = 0; I < al.size (); i++) {Clerk Clerk=(Clerk) Al.get (i); if(Clerk.getno (). Equals (No)) {System.out.println ("Find this employee information!" "); System.out.println ("Name:" +clerk.getname ()); System.out.println ("Age:" +clerk.getage ()); System.out.println ("Salary:" +clerk.getsalary ()); }        }    }     Public voidUpdatesalary (String No,floatsalary) {         for(inti = 0; I < al.size (); i++) {Clerk Clerk=(Clerk) Al.get (i); if(Clerk.getno (). Equals (No)) {clerk.setsalary (salary); }        }    }     Public voidRemoveclerk (String no) { for(inti = 0; I < al.size (); i++) {Clerk Clerk=(Clerk) Al.get (i); if(Clerk.getno (). Equals (No)) {al.remove (i); }        }    }}classClerk {PrivateString No; PrivateString name; Private intAge ; Private floatsalary;  PublicClerk (String No, string name,intAgefloatsalary) {         This. No =No;  This. Name =name;  This. Age =Age ;  This. Salary =salary; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public floatgetsalary () {returnsalary; }     Public voidSetsalary (floatsalary) {         This. Salary =salary; }     PublicString Getno () {returnNo; }     Public voidSetno (String no) { This. No =No; }}
ArrayList Sample Code

LinkedList, Vector, Stack and ArrayList are similar, without the concept of key value;
ArrayList and vectors are both Java collection classes that can be used to store Java objects, which are their same point, and their differences are as follows:
① synchronization, vectors are synchronous, and ArrayList are asynchronous, so objects in ArrayList are not thread-safe. Because the requirements of synchronization affect the efficiency of execution, if you do not need a thread-safe collection, then using ArrayList is a good option to avoid unnecessary performance overhead due to synchronization, which can improve efficiency.
② data growth, when you add elements to both types, if the number of elements exceeds the current length of the internal array, they all need to extend the length of the internal array,vector By default automatically increases the original array length, ArrayList is the original 50%, So in the end you get this set that takes up more space than you actually need, so if you want to save a lot of data in the collection, there are some advantages to using vectors, because you can set the initialization size of the collection to avoid unnecessary resource overhead.

2, the map structure of the collection class
HashMap class, Hashtable class

 Public classTest { Public Static voidMain (String args[]) {HashMap HM=NewHashMap (); Clerk Clerk1=NewClerk ("01", "Song Jiang", 40, 20000); Clerk Clerk2=NewClerk ("02", "Wu use", 38, 18000); Clerk Clerk3=NewClerk ("03", "Lin", 42, 15000); Hm.put ("01", CLERK1); Hm.put ("02", CLERK2); Hm.put ("03", CLERK3); if(Hm.containskey ("01") {System.out.println ("The staff was very friendly."); Clerk Clerk= (Clerk) hm.get ("01"); System.out.println ("Name:" +clerk.getname ()); System.out.println ("Age:" +clerk.getage ()); System.out.println ("Salary:" +clerk.getsalary ()); } Else{System.out.println ("No man!"); }        //traverse all keys and value in the HashMapIterator it =Hm.keyset (). iterator ();  while(It.hasnext ()) {//Remove KeyString key =It.next (). toString (); Clerk Clerk=(Clerk) Hm.get (key); System.out.println ("Name:" +clerk.getname ()); System.out.println ("Age:" +clerk.getage ()); System.out.println ("Salary:" +clerk.getsalary ()); }    }}
HashMap Sample Code

HashMap and Hashtable are both Java collection classes that can be used to store Java objects, which are their same point, but they also differ:
①hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2.
②hashtable is synchronous, and some methods of this class ensure that objects in Hashtable are thread-safe, and HashMap is asynchronous, so objects in HashMap are not thread-safe. Because the requirements of synchronization affect the efficiency of execution, if you do not need a thread-safe collection, then using HashMap is a good option to avoid unnecessary performance overhead due to synchronization, which can improve efficiency.
③hashmap can let you use NULL as the key or value of an entry for a table, but Hashtable cannot be placed in a null value (NULL)

3, set structure of the Set class
HashSet class, TreeSet class

4. Collection of queue structures
Queue interface

Summarize:
1, if the requirements of thread safety, the use of vector,hashtable;
2, if not require thread safety, should use ARRAYLIST,LINKEDLIST,HASHMAP;
3, if the key value pair is required, then use hashmap,hashtable;
4, if the amount of data is very large, but also thread safety considerations vector;

Java from getting started to mastering 09-Collections

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.