SUMMARY | Data structures in Java

Source: Internet
Author: User

String

    • The string class is non-modifiable, creating strings that need to be modified using StringBuffer (thread synchronization, higher security) or StringBuilder (thread is unsynchronized and faster).
    • You can connect a string with "+".
    • Creates a formatted string with String.Format ().

ArrayList

    • dynamic Array, which can dynamically increase or decrease the element.
    • Common operations:
      • Create:arraylist<type> arraylist=new arraylist<> ();
      • adding elements: Add
      • get element: Get
      • Delete element: Remove
      • Get array size: size
      • determine if an element is included: Contains
      • set element: Set
      • empty array: Clear
      • to determine if it is empty: IsEmpty
    • Traversal by: Indexing index is the most efficient
    •  type temp;  for  (int  i=0;i<arraylist.size () i++) {Temp
       =arraylist.get (i);}  
    • ArrayList ordering requires implementing the comparator interface for custom sorting
    • comparator<string> comparator=New comparator<string>() {    @Override      public int  Compare (String O1, String O2) {        ifreturn 1;         if return -1;         return 0;}    };

Stack

  • Expanded by vector.
  • Common operation: Push,peak,pop,search,isempty
  •  Public Static voidstacktest () {Stack<String> stack=NewStack<string>(); //pressing elements into the top of the stackSystem.out.println (Stack.push ("haha")); System.out.println (Stack.push ("Yes")); //The search result for the top element of the stack is 1 and then increments from the top of the stack to the bottom of the stackSystem.out.println (Stack.search ("haha")); //view top of stack elementsSystem.out.println (Stack.peek ()); //pops the top element of the stack and returns the top element of the stackSystem.out.println (Stack.pop ()); //determine if the stack is non-emptySystem.out.println (Stack.isempty ());}

Queue

  • is an interface that LinkedList implements the queue interface
  • Common operation: Offer,peek,poll,isempty
  •  Public Static voidqueuetest () {//LinkedList implements the queue interface, so it is constructed with LinkedListQueue<string> queue=NewLinkedlist<string>(); //increments the element at the end of the queue and returns whether it adds successSystem.out.println (Queue.offer ("abc"))); System.out.println (Queue.offer ("Def")); //returns the first element of the queue, which returns null if it is empty; (Ps:element throws an exception when the queue is empty)System.out.println (Queue.peek ()); //POPs the first element of the team and returns the first element of the team, returning null if the queue is emptySystem.out.println (Queue.poll ()); //determine if the queue is emptySystem.out.println (Queue.isempty ());}

LinkedList

  • Doubly linked list, non-synchronous
  • Common operation: Addfirst,addlast,add,getfirst,getlast,get,removefirst,removelast,remove,isempty
     Public Static voidlinkedlisttest () {LinkedList<String> linkedlist=NewLinkedlist<string>(); //in the chain list head Plus, tail Plus, any position Canadian dollarLinkedlist.addfirst ("haha"); Linkedlist.addlast ("Yali"); Linkedlist.add (1, "good"); //Output Link ListSystem.out.println ("first element:" +Linkedlist.getfirst ()); System.out.println ("Last element:" +linkedlist.getlast ());  for(intI=0;i<linkedlist.size (); i++) System.out.println (Linkedlist.get (i)); //Delete any position, head, tail of the linked list elementLinkedlist.remove (1);    Linkedlist.removefirst ();    Linkedlist.removelast (); //determine if the linked list is emptySystem.out.println (Linkedlist.isempty ());}

HashMap

  • Non-synchronous
  •  Public Static voidhashmaptest () {HashMap<string, integer> hashmap=NewHashmap<string, integer>(); //adding elements to the hash tableHashmap.put ("TP", 123); Hashmap.put ("ZP", 567);    System.out.println (HASHMAP); //facilitates a hash table with iterators     for(Iterator<entry<string, Integer>> iterator=Hashmap.entryset (). iterator (); Iterator.hasnext ();) {Entry<string, integer> entry=Iterator.next ();        System.out.println (Entry.getkey ());    System.out.println (Entry.getvalue ()); }    //find if the key is in a hash tableSystem.out.println (Hashmap.containskey ("he"))); //find if the hash table has this valueSystem.out.println (Hashmap.containsvalue (567)); //Delete a key-value pair from a hash tableHashmap.remove ("TP"); System.out.println (HASHMAP);}

SUMMARY | Data structures in Java

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.