Java8和Java7的Map和List遍歷對比總結

Java8和Java7的Map和List遍歷對比總結

一、List遍歷

public class LambdaList {
 
    private List<String> list = new ArrayList<>();
 
    @Before
    public void initData() {
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Four");
        list.add("Five");
        list.add("Six");
        list.add("Seven");
        list.add("Eight");
        list.add("Nine");
        list.add("Ten");
    }
 
    /**
     * 在Java7的遍歷方式
     */
    @Test
    public void testErgodicWayBeforeJava8() {
        System.out.println("ErgodicWayOne------> fori");
        for (int i = 0; i < list.size(); i++) {
            System.out.println("list.get(i) = " + list.get(i));
        }
        System.out.println("ErgodicWayOne------> foreach");
        for (String ele : list) {
            System.out.println("ele = " + ele);
        }
        System.out.println("ErgodicWayOne------> iterator");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println("iterator.next() = " + iterator.next());
        }
    }
 
    /**
     * 在Java8中的遍歷方式
     */
    @Test
    public void testErgodicWayJava8() {
        list.forEach(str-> System.out.println(str))
    }
}

二、Map遍歷

public class LambdaMap {

    private Map<String, Object> map = new HashMap<>();

    @Before
    public void initData() {
        map.put("key1", "One");
        map.put("key2", "Two");
        map.put("key3", "Three");
        map.put("key4", 4);
        map.put("key5", 5);
    }


    /**
     * 遍歷Map的方式一
     * 通過Map.keySet遍歷key和value
     */
    @Test
    public void testErgodicWayOne() {
        System.out.println("---------------------JAVA7 ------------------------------");
        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("--------------------- JAVA7 ------------------------------");
        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("--------------------- JAVA7 ------------------------------");
        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("--------------------- JAVA7 ------------------------------");
        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() {
        map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
    }
}

未完,待完善

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