java基礎知識5--集合類(Set,List,Map)和迭代器Iterator的使用

來源:互聯網
上載者:User

標籤:csdn   cas   compareto   總結   collect   hashcode   pac   16px   遍曆   

寫的非常棒的一篇總結: http://blog.csdn.net/speedme/article/details/22398395#t1

下面主要看各個集合如何使用迭代器Iterator擷取元素:

1.list使用迭代器Iterator

public class testArrayList {    public static void main(String args[]) {        f1();        f2();    }    public static void f1(){        List<String> strList = new ArrayList<String>();        for (int i = 0; i < 10; i++)        {            strList.add("string" + i);        }        Iterator<String> iterator = strList.iterator();//list.iterator()        while (iterator.hasNext())  //判斷集合中元素是否遍曆完畢,如果沒有,就返回true        {            if (iterator.next().equals("string3"))            {                iterator.remove();  //iterator.remove()移除的是最近一次iterator.next()所擷取的對象            }        }        iterator = strList.iterator();//此步非常重要,刪除後要重新定義迭代器        while (iterator.hasNext())        {            System.out.println(iterator.next()); //iterator.next() 返回下一個元素        }    }        public static void f2() {// 1.向arraylist中添加元素            ArrayList arrayList = new ArrayList();            arrayList.add("wyy");  //arrayList.add            arrayList.add("12");            arrayList.add("huhu");            arrayList.add("897");            System.out.println("輸出整個arraylist" + arrayList);  //[wyy, 12, huhu, 897]            for (int i = 0; i < arrayList.size(); i++) {                System.out.println(arrayList.get(i));            }            Iterator<String> a = arrayList.iterator();            System.out.println("剩下的元素有:");            while (a.hasNext()) {                if (a.next().equals("12")) {                    a.remove();                }            }//用for迴圈,可以節約記憶體,開發時一般使用            for( a = arrayList.iterator();a.hasNext();)//注意,如果判斷結果後刪除,必須重新對迭代器進行定義,才能取出剩餘的資料            {                System.out.println(a.next());            }        }}

2.set使用迭代器Iterator

package collection;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import java.util.TreeSet;/** * Created by wyy on 2017/2/20. */public class SetTest {    public static void main(String[] args) {//set常用的方法:        Set set = new HashSet();        set.add("wq");        set.add("er");        set.add("er");        set.add("gee");        set.add("wdd");        set.add("3242");        System.out.println(set);        Set sortedSet = new TreeSet(set); //添加的元素必須是有效,即可比較排序的,用compareTo     /* sortedSet.add(new SetTest());        報錯:collection.SetTest cannot be cast to java.lang.Comparable  add的元素必須是可以比較的*/        System.out.println(sortedSet); //[3242, er, gee, wdd, wq],列表的第二次輸出已按字母順序排序。        String a="a";        String b="a";        System.out.println(a.hashCode());//65        System.out.println(b.hashCode());//97        Iterator iterator=set.iterator();//建立一個集合的迭代器,用來取元素        while (iterator.hasNext()){              Object obj = iterator.next();            System.out.println(obj);        }    }}

3.map使用迭代器,共有4種方法

package collection;import java.util.*;/** * Created by wyy on 2017/2/21. */public class MapTest {    public static void main(String[] args) {        Map<Integer,String> map=new HashMap<>();        map.put(1,"wo");        map.put(2,"chi");        map.put(3,"xi");        map.put(4,"gua");        System.out.println(map.size());/*方法1:使用set儲存key的值,然後用for迴圈擷取value的值*/        Set<Integer> set=map.keySet();//擷取所有key的值        for (int i:set             ) {            String value=map.get(i);  //擷取value的值            System.out.println(value);        }/*方法2:使用Map.entrySet<>構造iterator遍曆key和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());        }/*方法3:推薦,通過Map.entrySet遍曆key和value,特別是大容量時 */        for (Map.Entry<Integer,String> en:map.entrySet()){            System.out.println("the key is:"+en.getKey()+",and the value is:"+en.getValue());        }        /* 方法4:只能得到value的值       */        for (String v1:map.values()             ) {            System.out.println("the value is:"+v1);        }    }}

 

java基礎知識5--集合類(Set,List,Map)和迭代器Iterator的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.