Simple use of Java collections

Source: Internet
Author: User

/*

List Interface
ArrayList uses asynchronous processing for high performance but is non-thread safe
Vector uses synchronous processing, low performance, but is thread-safe
When the program considers thread safety, choose vector, Other choice ArrayList, fast, high performance

The set interface cannot add duplicate elements, (memory: s starts same instead), but can be sorted
Common subclasses of the set interface
Hash Deposit: HashSet (hash words are not sorted)
Orderly storage: TreeSet (ordered)

Iterator interface
Standard operation for collection output, using the iterator interface
Operating principle: The output of the iteration is to determine whether the elements are content, if there is content to remove the content

Map interface
Key--->value way to save
Common sub-categories
HASHMAP: unordered storage, key not allowed to repeat
Hashtable: unordered storage, key not allowed to repeat


Public Static voidMain (string[] args) {//list added data can be repeated, non-thread safe, fastList<string> list=NULL; List<String> list1=NULL; List=NewArraylist<>(); List.add (A); List.add (A); List.add (B); SYSTEM.OUT.PRINTLN (list);//[A, A, B] for(inti = 0; I <list.size (); i++) {System.out.println (List.get (i)); //A//A//B} System.out.println ("After removing the first element is"); List.remove (0); for(inti = 0; I <list.size (); i++) {System.out.println (List.get (i)); //Print A B at this time} System.out.println ("Element is empty" +list.isempty ());//falseSystem.out.println (List.indexof ("A"));//at this point the subscript for a is 0System.out.println ("----------------------------"); List1=NewVector<> ();//thread safe, but slowList1.add ("a"); List1.add ("B"); List1.add (C); for(inti = 0; I <list1.size (); i++) {System.out.println (List1.get (i)); //print a B C at this time} System.out.println ("----------------------------"); Set<String> set=NewHashset<>(); Set.add (A); Set.add (B); Set.add (C); Set.add (D); Set.add (E); SYSTEM.OUT.PRINTLN (set);//[D, E, A, B, C] No orderSet=NewTreeset<>(); Set.add (A); Set.add ("B"); Set.add (C); Set.add ("D"); Set.add (E); SYSTEM.OUT.PRINTLN (set);//[A, B, C, D, E] in orderSystem.out.println ("----------------------------"); List<string>list3=NewArraylist<>(); List3.add ("Xiaoli"); List3.add ("Koky"); List3.add ("Lucy"); List3.add ("Jack"); //System.out.println (LIST3);Iterator<string> iterator=List3.iterator (); while(Iterator.hasnext ()) {String str=Iterator.next (); if("Lucy". Equals (str)) { //List3.remove (2);//Error iteration output cannot list delete elementIterator.remove ();//Removal of iterations } Else { //System.out.println (Iterator.next ()); System.out.println (str); }} System.out.println ("---------------------"); Map<string, string>map=NewHashmap<>(); Map.put ("Name", "Jack");//map words added by put, not list,set addMap.put ("Address", "Beijing"); Map.put ("Phone", "123456"); String Key=map.get ("name");//by taking keySYSTEM.OUT.PRINTLN (key);//Output Value//How do you determine if key or value exists? if(Map.containskey ("Address") {System.out.println ("Key exists"); } Else{System.out.println ("Key does not exist"); } if(Map.containsvalue ("Beijing1") {System.out.println ("Value exists"); } Else{System.out.println ("Value does not exist"); } //How do I take out all the keys and value? set<string>s=Map.keyset (); Iterator<String> i=S.iterator (); while(I.hasnext ()) {System.out.println (I.next ()); //Print all key, because it is set, no order} Collection<string>c=map.values (); Iterator<String> i1=C.iterator (); while(I1.hasnext ()) {System.out.println (I1.next ()); //Print all the value } }}

Simple use of Java 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.