map的三種遍曆方法!

來源:互聯網
上載者:User
map的三種遍曆方法!

  集合的一個很重要的操作---遍曆,學習了三種遍曆方法,三種方法各有優缺點~~

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.tsp2c.liubao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 *
 * @author Administrator
 */
public class TestMap {

    public static void main(String[] args) {
        Map<String, Student> map = new HashMap<String, Student>();
        Student s1 = new Student("宋江", "1001", 38);
        Student s2 = new Student("盧俊義", "1002", 35);
        Student s3 = new Student("吳用", "1003", 34);
        
        map.put("1001", s1);
        map.put("1002", s2);
        map.put("1003", s3);

        Map<String, Student> subMap = new HashMap<String, Student>();
        subMap.put("1008", new Student("tom", "1008", 12));
        subMap.put("1009", new Student("jerry", "1009", 10));
        map.putAll(subMap);

        work(map);
        workByKeySet(map);
        workByEntry(map);
    }

  //最常規的一種遍曆方法,最常規就是最常用的,雖然不複雜,但很重要,這是我們最熟悉的,就不多說了!!

    public static void work(Map<String, Student> map) {
        Collection<Student> c = map.values();
        Iterator it = c.iterator();
        for (; it.hasNext();) {
            System.out.println(it.next());
        }
    }

  //利用keyset進行遍曆,它的優點在於可以根據你所想要的key值得到你想要的 values,更具靈活性!!

    public static void workByKeySet(Map<String, Student> map) {
        Set<String> key = map.keySet();
        for (Iterator it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
            System.out.println(map.get(s));
        }
    }

  //比較複雜的一種遍曆在這裡,呵呵~~他很暴力哦,它的靈活性太強了,想得到什麼就能得到什麼~~

    public static void workByEntry(Map<String, Student> map) {
        Set<Map.Entry<String, Student>> set = map.entrySet();
        for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) {
            Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next();
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }
    }
}

class Student {

    private String name;
    private String id;
    private int age;

    public Student(String name, String id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "name=" + name + "id=" + id + "age=" + age + '}';
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.