使用Iterator迭代器遍歷集合數據

import java.util.Collection;
import java.util.*;

/**
 * @author mhSui
 * @create 2018-06-06 8:42
 */
public class Test2{
    public static void main(String[] args) {

        ArrayList<String> list=new ArrayList<>();
        //集合
        list.add("張三");
        list.add("李四");
        list.add("王二");
        list.add("佳明");

        /**
         * Iterator迭代器
         * 1.獲取迭代器
         */
        Iterator<String> iter=list.iterator();
        /**
         * 2.通過循環迭代
         * hasNext():判斷是否存在下一個元素
         */
        while (iter.hasNext()){
            /**
             * 如果存在,則調用next實現迭代
             * Object-->String
             */
            String lis=iter.next();
            System.out.println(lis);
        }
    }
}

//使用迭代器Iterator遍歷Set集合
class Theme{
    private int id;
    private String title;
    private String remark;

    public Theme(){}
    public Theme(int id,String title,String remark){
        this.id=id;
        this.title=title;
        this.remark=remark;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}
class IteratorSet{
    public static void main(String[] args) {
        //存儲數據的地址
        Set set =new HashSet<>();
        //存儲數據
        set.add(new Theme(1,"標題1","簡介:隨夢豪是大帥哥!"));
        set.add(new Theme(2,"標題2","簡介:隨夢豪是大帥哥!"));
        //遍歷數據
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            Theme theme=(Theme) iterator.next();
            System.out.println(theme.getId()+theme.getTitle()+theme.getRemark());
        }
    }
}

//使用迭代器Iterator遍歷Map集合
class IteratorMap{
    public static void main(String[] args) {
        Map map=new HashMap<>();
        map.put(1,"王二");
        map.put(2,"張三");

        //所有鍵值對中的鍵,組成一個集合
        Set set=map.keySet();
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            //打印出map中的鍵(1,2)
            System.out.println(iterator.next());
        }

        //打印出值
        //values所有值組成的一個集合
        Collection collections=map.values();
        //重寫了toString方法
        //打印出王二、張三
        System.out.println(collections);
    }
}


class IteratorMap2{
    public static void main(String[] args) {
        Map map=new HashMap<>();
        map.put(1,"s");
        map.put(2,"m");
        map.put(3,"h");
        /**
         * 必須掌握
         * 所有鍵值對中的鍵,組成一個set集合
         */
        Set set=map.keySet();
        System.out.println(set);
        //values所有的值組成一個集合
        Collection collection=map.values();
        System.out.println(collection);

        /**
         * 獲取所有的鍵和值
         * entrySet可以得到由所有鍵值對組成的集合
         * 裏面存儲的是所有的數據(鍵-值)
         */
        Set<Map.Entry<Integer,String>> entrySet=map.entrySet();
        Iterator<Map.Entry<Integer,String>> iterator=entrySet.iterator() ;
        while (iterator.hasNext()){
            Map.Entry<Integer,String> entry=iterator.next();
            System.out.println("鍵:"+entry.getKey()+"值:"+entry.getValue());
        }
    }
}
/**
 * @author mhSui
 * @create 2018-06-06 8:42
 */
public class Test2{
    public static void main(String[] args) {

        ArrayList<String> list=new ArrayList<>();
        //集合
        list.add("張三");
        list.add("李四");
        list.add("王二");
        list.add("佳明");

        /**
         * Iterator迭代器
         * 1.獲取迭代器
         */
        Iterator<String> iter=list.iterator();
        /**
         * 2.通過循環迭代
         * hasNext():判斷是否存在下一個元素
         */
        while (iter.hasNext()){
            /**
             * 如果存在,則調用next實現迭代
             * Object-->String
             */
            String lis=iter.next();
            System.out.println(lis);
        }
    }
}

//使用迭代器Iterator遍歷Set集合
class Theme{
    private int id;
    private String title;
    private String remark;

    public Theme(){}
    public Theme(int id,String title,String remark){
        this.id=id;
        this.title=title;
        this.remark=remark;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}
class IteratorSet{
    public static void main(String[] args) {
        //存儲數據的地址
        Set set =new HashSet<>();
        //存儲數據
        set.add(new Theme(1,"標題1","簡介:隨夢豪是大帥哥!"));
        set.add(new Theme(2,"標題2","簡介:隨夢豪是大帥哥!"));
        //遍歷數據
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            Theme theme=(Theme) iterator.next();
            System.out.println(theme.getId()+theme.getTitle()+theme.getRemark());
        }
    }
}

//使用迭代器Iterator遍歷Map集合
class IteratorMap{
    public static void main(String[] args) {
        Map map=new HashMap<>();
        map.put(1,"王二");
        map.put(2,"張三");

        //所有鍵值對中的鍵,組成一個集合
        Set set=map.keySet();
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            //打印出map中的鍵(1,2)
            System.out.println(iterator.next());
        }

        //打印出值
        //values所有值組成的一個集合
        Collection collections=map.values();
        //重寫了toString方法
        //打印出王二、張三
        System.out.println(collections);
    }
}


class IteratorMap2{
    public static void main(String[] args) {
        Map map=new HashMap<>();
        map.put(1,"s");
        map.put(2,"m");
        map.put(3,"h");
        /**
         * 必須掌握
         * 所有鍵值對中的鍵,組成一個set集合
         */
        Set set=map.keySet();
        System.out.println(set);
        //values所有的值組成一個集合
        Collection collection=map.values();
        System.out.println(collection);

        /**
         * 獲取所有的鍵和值
         * entrySet可以得到由所有鍵值對組成的集合
         * 裏面存儲的是所有的數據(鍵-值)
         */
        Set<Map.Entry<Integer,String>> entrySet=map.entrySet();
        Iterator<Map.Entry<Integer,String>> iterator=entrySet.iterator() ;
        while (iterator.hasNext()){
            Map.Entry<Integer,String> entry=iterator.next();
            System.out.println("鍵:"+entry.getKey()+"值:"+entry.getValue());
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章