Java Training-Collections

Source: Internet
Author: User
Tags ming set set

  1. Collection Framework Architecture

    650) this.width=650; "src=" http://static.oschina.net/uploads/space/2015/0714/171133_0wqx_2414224.jpg "alt=" 171133 _0wqx_2414224.jpg "/>

  2. The collection Framework Architecture diagram explains:

    Virtual Point box: Represents an interface, a dashed box: represents an abstract interface, a solid wireframe: A class that represents the implementation interface

  3. The difference between collection and collections:

    Collection is the ancestor interface of the collection class, and the main interface for inheriting it is set and list.
    Collections is a helper class for a collection class that provides a series of static methods for searching, sorting, and line of various collections
    Process safety and other operations.

  4. Map Common Sub-categories

    Hashtable: Hash table, is synchronous, does not allow null keys and null values

    HashMap: Hash table, is out of sync, allows null key and null value

    TreeMap: Two fork tree, out of sync, can sort keys in the map collection

  5. Collection interface common subclass set and list:

    Set: Do not record the order in which elements are saved and duplicate data is not allowed

    List: Records the order in which elements are saved and allows duplicate data.

  6. Arraylist,vector, LinkedList storage performance and features

    Both ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement, so the index data is fast and the data is inserted slowly. Vector because of the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList, and LinkedList using a doubly linked list for storage, index data by ordinal need to be forward or backward traversal, but when inserting data only need to record the item before and after items can be , so the insertion speed is faster.

  7. ArrayList: By using the array implementation, the problem of fixed length to variable length and type diversification in arrays is solved.

    Iterator: Iterator interface method: Hasnext () Determines if there is a value in the iterator, next () takes a value and moves the pointer to the next single

  8. Summarize:

    If it involves operations such as stacks, queues, and so on, you should consider using the list, for quick insertions, for deleting elements, should use LinkedList, and if you need to quickly randomly access elements, you should use ArrayList.
    If the program is in a single-threaded environment, or if access is done only in one thread, it is more efficient to consider non-synchronous classes, and if multiple threads may operate on a class at the same time, the synchronized classes should be used.
    Pay special attention to the operation of the hash table, and the object as key should correctly replicate the Equals and Hashcode methods.
    Try to return the interface rather than the actual type, such as returning a list instead of ArrayList, so that if you need to change ArrayList to LinkedList later, the client code does not have to be changed. This is for abstract programming.

  9. Take the value in ArrayList:

Public static void test1 () {        arraylist alist =new arraylist ();         alist.add (5);         alist.add (2);         alist.add (0);         alist.add ("i");         alist.add (' A ');         //for cycle Value          for  (Int i = 0; i < alist.size ();  i++)  {             object obj=alist.get (i);             system.out.println (obj);         }        //iterator Iterator Value          iterator&Nbsp;it=alist.iterator ();         while (It.hasNext ()) {             object obj=it.next ();         }        //iterator Plus for loop fetch value          for  (Iterator iterator = alist.iterator ();  iterator.hasnext () ;)  {            object object = Iterator.next ();                      }    }

10. Take the key-value pairs in the Map collection: Key key is stored with set set, value is stored with collection collection, key value pair is stored with set set

Public static void putelements () {        hashmap.put (" Huang ", " Ming ")         hashmap.put (" Chao ", " Sheng ");         hashmap.put ("Shi",  "tian");         int size=hashmap.size ();         system.out.println (" Size= "+size);     }    //key set Collection     public  static void getkey () {        set keyset= Hashmap.keyset ();         iterator it=keyset.iterator ();         while (It.hasnext ()) {             object key=it.next ();             system.out.println ("keys=" +key);        }    }     //value collection Interface     public static void getvalues () {         collection coll=hashmap.values ();         iterator it=coll.iterator ();        for  ( Iterator iterator = coll.iterator ();  iterator.hasnext ();)  {             object value = iterator.next ();             system.out.println ("values=" +value);         }    }    //key-value  EntrySet set &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;GETKV () {         //Way One   Enhanced for Loop get         for (Object obj:hashmap.entryset ()) {             entry entry= (Entry)  obj;             object key=entry.getkey ();             object value=entry.getvalue ();             system.out.println ("key=" +key+ "; value=" +value);         }                  //mode two   get value        for via key (Object obj:hashmap.keyset ()) {            object key=obj;             object value=hashmap.get (obj);  &nbSp;          system.out.println ("key=" +key+ "; value=" +value);         }                  //Mode III   get key and value       through internal class entry   set entryset=hashmap.entryset ();         iterator  it=entryset.iterator ();         while (It.hasNext ()) {             entry entry= (Entry)  it.next ();             object key=entry.getkey ();             object value=entry.getvalue ();             system.out.println ("key=" +key+ "; value=" +value);         }    } 

11.Stack class:

Stack     public static void test () {         stack stack=new stack ();         stack.add ("Apple ");         stack.add (" banana ");         stack.add (123);         stack.add (New Date ());                  Iterator  It=stack.iterator ();         while (It.hasnext ()) {             object obj=it.next ();             system.out.println (obj);        The  }        //peek method is to take a value that does not change the stack size          object p Eekobj=stack.peek ();         system.out.println ("PEEKOBJ:" +PEEKOBJ);         system.out.println ("Peek after:" +stack.size ());  The        //pop method pops the last value and changes the size of the stack          object popobj=stack.pop ();         system.out.println (" Popobj: "+popobj");         system.out.println ("Pop after:" + Stack.size ());              }

12. Count the number of each character in the input statement:

Public static void countchar () {        scanner sc= New scanner (system.in);         system.out.println ("Please enter the statement to be counted:");         string s=sc.next ();         try {            //first uses GBK to decode the characters, Then use UTF-8 to raise the string.             s = new string ( S.getbytes ("GBK"),  "Utf-8");             System.out.println (s);        } catch  ( Unsupportedencodingexception e)  {             e.printstacktrace ();        }         char[] c=s.tocharaRray ();         map<character,integer> map=new treemap <Character,Integer> (;        for ) (int i = 0 ;  i < c.length; i++)  {             if (! ( Map.containskey (C[i])) {                 map.put (c[i], 1);             } Else{                map.put (c [I], map.get (C[i]) +1);            }         }                  for (Object obj:map.entryset ()) {             entry entry= (Entry)  obj;             object key=entry.getkey ();             object value=entry.getvalue ();             system.out.println (key+ "=" +value);         }     }


This article is from the "Ming" blog, make sure to keep this source http://8967938.blog.51cto.com/8957938/1676505

Java Training-Collections

Related Article

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.