欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java8之集合(Collection)遍历 forEach()、stream()

程序员文章站 2022-05-10 15:31:06
...
package java8.collections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;

public class ForEach {
    
    private List<String> list = new ArrayList<>();
 
    @Before
    public void initData() {
        list.add("Java 1.5");
        list.add("Java 1.6");
        list.add("Scala");
        list.add("Python");
        list.add("Html");
        list.add("css");
        list.add("JavaScript");
        list.add("js");
        list.add("Hadoop");
        list.add("C");
        list.add("redis");
        list.add("Java 1.7");
        list.add("Java 1.8");
 
    }
 
    /**
     * 在Java8之前的遍历方式
     */
    @Test
    public void testErgodicWayBeforeJava8() {
        System.out.println("\nBefore JAVA8 ===================================>");
        System.out.println(">>>> for loop:");
        for (int i = 0; i < list.size(); i++) {
            System.out.println("list.get(i) = " + list.get(i));
        }
        System.out.println(">>>> for loop enhanced:");
        for (String ele : list) {
            System.out.println("ele = " + ele);
        }
        System.out.println(">>>> iterator:");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println("iterator.next() = " + iterator.next());
        }
    }
 
    /**
     * 在Java8中的遍历方式
     */
    @Test
    public void testCollection_forEach_Java8() {
        System.out.println("\nJAVA8:::Collection.forEach ======================>");
        list.forEach(System.out::println);
    }
    
    @Test
    public void testCollection_stream_map_Java8() {
        System.out.println("\nJAVA8:::Collection.stream.map ==================>");
        list.stream()
            .map(item -> "Mapped_Value_" + item)
            .collect(Collectors.toList()).stream()
            .forEach(System.out::println);
    }
    
    @Test
    public void testCollection_stream_filter_map_Java8() {
        System.out.println("\nJAVA8:::Collection.stream.filter.map ==========>");
        list.stream()
            .filter(item -> item.contains("Java"))
            .map(item -> "Mapped_Value_" + item)
            .collect(Collectors.toList()).stream()
            .forEach(System.out::println);
    }
    
}



输出结果:


Before JAVA8 ===================================>
>>>> for loop:
list.get(i) = Java 1.5
list.get(i) = Java 1.6
list.get(i) = Scala
list.get(i) = Python
list.get(i) = Html
list.get(i) = css
list.get(i) = JavaScript
list.get(i) = js
list.get(i) = Hadoop
list.get(i) = C
list.get(i) = redis
list.get(i) = Java 1.7
list.get(i) = Java 1.8
>>>> for loop enhanced:
ele = Java 1.5
ele = Java 1.6
ele = Scala
ele = Python
ele = Html
ele = css
ele = JavaScript
ele = js
ele = Hadoop
ele = C
ele = redis
ele = Java 1.7
ele = Java 1.8
>>>> iterator:
iterator.next() = Java 1.5
iterator.next() = Java 1.6
iterator.next() = Scala
iterator.next() = Python
iterator.next() = Html
iterator.next() = css
iterator.next() = JavaScript
iterator.next() = js
iterator.next() = Hadoop
iterator.next() = C
iterator.next() = redis
iterator.next() = Java 1.7
iterator.next() = Java 1.8

JAVA8:::Collection.forEach ======================>
Java 1.5
Java 1.6
Scala
Python
Html
css
JavaScript
js
Hadoop
C
redis
Java 1.7
Java 1.8

JAVA8:::Collection.stream.map ==================>
Mapped_Value_Java 1.5
Mapped_Value_Java 1.6
Mapped_Value_Scala
Mapped_Value_Python
Mapped_Value_Html
Mapped_Value_css
Mapped_Value_JavaScript
Mapped_Value_js
Mapped_Value_Hadoop
Mapped_Value_C
Mapped_Value_redis
Mapped_Value_Java 1.7
Mapped_Value_Java 1.8

JAVA8:::Collection.stream.filter.map ==========>
Mapped_Value_Java 1.5
Mapped_Value_Java 1.6
Mapped_Value_JavaScript
Mapped_Value_Java 1.7
Mapped_Value_Java 1.8



















=