Java Basics 5--Collection Class (Set,list,map) and iterator iterator use

Source: Internet
Author: User

Write a very good summary: http://blog.csdn.net/speedme/article/details/22398395#t1

Here's a look at how each collection uses an iterator iterator to get an element:

1.list using iterators iterator

 Public classTestarraylist { Public Static voidMain (String args[]) {F1 ();    F2 (); }     Public Static voidF1 () {List<String> strlist =NewArraylist<string>();  for(inti = 0; I < 10; i++) {Strlist.add ("String" +i); } Iterator<String> iterator = Strlist.iterator ();//List.iterator ()         while(Iterator.hasnext ())//determines whether the elements in the collection are traversed, and if not, returns True        {            if(Iterator.next (). Equals ("String3") ) {iterator.remove (); //iterator.remove () removes the object obtained from the most recent iterator.next ()}} iterator= Strlist.iterator ();//This step is important to redefine the iterator after deletion         while(Iterator.hasnext ()) {System.out.println (Iterator.next ());//Iterator.next () returns the next element        }    }         Public Static voidF2 () {//1. Adding elements to the ArrayListArrayList ArrayList=NewArrayList (); Arraylist.add ("Wyy");//Arraylist.addArraylist.add ("12"); Arraylist.add ("Huhu"); Arraylist.add ("897"); System.out.println ("Output entire arrayList" + arrayList);//[Wyy, Huhu, 897]             for(inti = 0; I < arraylist.size (); i++) {System.out.println (Arraylist.get (i)); } Iterator<String> A =Arraylist.iterator (); System.out.println ("The rest of the elements are:");  while(A.hasnext ()) {if(A.next (). Equals ("12") ) {a.remove (); }            }//Use for Loop, can save memory, general use when developing             for(A = Arraylist.iterator (); A.hasnext ();)//Note that if you delete the result after the decision, you must re-define the iterator to remove the remaining data{System.out.println (A.next ()); }        }}

2.set using iterators iterator

 Packagecollection;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Set;ImportJava.util.TreeSet;/*** Created by Wyy on 2017/2/20.*/ Public classSettest { Public Static voidMain (string[] args) {//common methods of set:Set set =NewHashSet (); Set.add ("Wq"); Set.add ("ER"); Set.add ("ER"); Set.add ("Gee"); Set.add ("Wdd"); Set.add ("3242");        SYSTEM.OUT.PRINTLN (set); Set SortedSet=NewTreeSet (set);//the added elements must be valid to compare the sort, with CompareTo     /*Sortedset.add (New Settest ()); Error: Collection. Settest cannot is cast to java.lang.Comparable add element must be comparable*/System.out.println (sortedSet);//[3242, er, gee, WDD, Wq], the second output of the list is sorted alphabetically. String a= "a"; String b= "a"; System.out.println (A.hashcode ());// $System.out.println (B.hashcode ());// theIterator Iterator=set.iterator ();//creates an iterator to the collection that takes the element         while(Iterator.hasnext ()) {Object obj=Iterator.next ();        System.out.println (obj); }    }}

3.map using iterators, there are 4 ways

 Packagecollection;ImportJava.util.*;/*** Created by Wyy on 2017/2/21.*/ Public classMaptest { Public Static voidMain (string[] args) {Map<Integer,String> map=NewHashmap<>(); Map.put (1, "Wo"); Map.put (2, "Chi"); Map.put (3, "XI"); Map.put (4, "Gua"); System.out.println (Map.size ());/*Method 1: Use set to store the value of key, and then use the For loop to get the value*/Set<Integer> Set=map.keyset ();//get the value of all keys         for(intI:set) {String value=map.get (i);//Gets the valueSystem.out.println (value); }/*Method 2: Use map.entryset<> construct iterator to traverse key and value*/Iterator<Map.Entry<Integer,String>> it=Map.entryset (). iterator ();  while(It.hasnext ()) {Map.entry<Integer,String> entry=It.next (); System.out.println ("The key is:" +entry.getkey () + ", and the value is:" +Entry.getvalue ()); }/*Method 3: Recommended to traverse key and value through Map.entryset, especially when large capacity*/         for(map.entry<integer,string>En:map.entrySet ()) {System.out.println ("The key is:" +en.getkey () + ", and the value is:" +En.getvalue ()); }        /*Method 4: You can only get values of value*/         for(String v1:map.values ()) {System.out.println ("The value is:" +v1); }    }}

Java Basics 5--Collection Class (Set,list,map) and iterator iterator use

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.