java 8 map遍曆方式 (轉)

來源:互聯網
上載者:User

標籤:string   put   value   map   foreach   ext   map遍曆   set   java   

public class LambdaMap {    private Map<String, Object> map = new HashMap<>();    @Before    public void initData() {        map.put("key1", "value1");        map.put("key2", "value2");        map.put("key3", "value3");        map.put("key4", 4);        map.put("key5", 5);        map.put("key5", ‘h‘);    }    /**     * 遍曆Map的方式一     * 通過Map.keySet遍曆key和value     */    @Test    public void testErgodicWayOne() {        System.out.println("---------------------Before JAVA8 ------------------------------");        for (String key : map.keySet()) {            System.out.println("map.get(" + key + ") = " + map.get(key));        }        System.out.println("---------------------JAVA8 ------------------------------");        map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));    }    /**     * 遍曆Map第二種     * 通過Map.entrySet使用Iterator遍曆key和value     */    @Test    public void testErgodicWayTwo() {        System.out.println("---------------------Before JAVA8 ------------------------------");        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();        while (iterator.hasNext()) {            Map.Entry<String, Object> entry = iterator.next();            System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());        }        System.out.println("---------------------JAVA8 ------------------------------");        map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));    }    /**     * 遍曆Map第三種     * 通過Map.entrySet遍曆key和value,在大容量時推薦使用     */    @Test    public void testErgodicWayThree() {        System.out.println("---------------------Before JAVA8 ------------------------------");        for (Map.Entry<String, Object> entry : map.entrySet()) {            System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());        }        System.out.println("---------------------JAVA8 ------------------------------");        map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));    }    /**     * 遍曆Map第四種     * 通過Map.values()遍曆所有的value,但不能遍曆key     */    @Test    public void testErgodicWayFour() {        System.out.println("---------------------Before JAVA8 ------------------------------");        for (Object value : map.values()) {            System.out.println("map.value = " + value);        }        System.out.println("---------------------JAVA8 ------------------------------");        map.values().forEach(System.out::println); // 等價於map.values().forEach(value -> System.out.println(value));    }    /**     * 遍曆Map第五種     * 通過k,v遍曆,Java8專屬的     */    @Test    public void testErgodicWayFive() {        System.out.println("---------------------Only JAVA8 ------------------------------");        map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));    }}

 

java 8 map遍曆方式 (轉)

聯繫我們

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