Java 8 Streams map() examples 修改教程

在 Java 8里面stream().map() 允许你把 object 转化成其他对象. 回顾一下下面的例子 :

1. 将一个List<String>里面的字符转换成大写字母

1.1 Java List<String>转换为大写字母.

TestJava8.java

package com.mkyong.java8;

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

public class TestJava8 {

    public static void main(String[] args) {

        List<string> alpha = Arrays.asList("a", "b", "c", "d");

        //Java8 之前的操作
        List<string> alphaUpper = new ArrayList&lt;&gt;();
        for (String s : alpha) {
            alphaUpper.add(s.toUpperCase());
        }

        System.out.println(alpha); //[a, b, c, d]
        System.out.println(alphaUpper); //[A, B, C, D]

        // Java 8 里面的操作
        List<string> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(collect); //[A, B, C, D]

        // 另外, streams 允许你做任意的转化.
        List<integer> num = Arrays.asList(1,2,3,4,5);
        List<integer> collect1 = num.stream().map(n -&gt; n * 2).collect(Collectors.toList());
        System.out.println(collect1); //[2, 4, 6, 8, 10]

    }

}

2. 从对象List 获取字符 List

2.1 从staff 对象List中获取所有 name 值.

Staff.java

package com.mkyong.java8;

import java.math.BigDecimal;

public class Staff {

    private String name;
    private int age;
    private BigDecimal salary;
    //...
}

TestJava8.java

package com.mkyong.java8;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestJava8 {

    public static void main(String[] args) {

        List<staff> staff = Arrays.asList(
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );

        //在 Java 8 之前
        List<string> result = new ArrayList&lt;&gt;();
        for (Staff x : staff) {
            result.add(x.getName());
        }
        System.out.println(result); //[mkyong, jack, lawrence]

        //Java 8
        List<string> collect = staff.stream().map(x -&gt; x.getName()).collect(Collectors.toList());
        System.out.println(collect); //[mkyong, jack, lawrence]

    }

}

3. objects List转化为 其他 objects List

3.1 这个例子展示如何将一个 staff 对象 List 转化为一个 StaffPublic 对象List.

Staff.java

package com.mkyong.java8;

import java.math.BigDecimal;

public class Staff {

    private String name;
    private int age;
    private BigDecimal salary;
    //...
}

StaffPublic.java

package com.mkyong.java8;

public class StaffPublic {

    private String name;
    private int age;
    private String extra;
    //...
}

3.2 Java 8之前.

BeforeJava8.java

package com.mkyong.java8;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BeforeJava8 {

    public static void main(String[] args) {

        List<staff> staff = Arrays.asList(
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );

        List<staffpublic> result = convertToStaffPublic(staff);
        System.out.println(result);

    }

    private static List<staffpublic> convertToStaffPublic(List<staff> staff) {

        List<staffpublic> result = new ArrayList&lt;&gt;();

        for (Staff temp : staff) {

            StaffPublic obj = new StaffPublic();
            obj.setName(temp.getName());
            obj.setAge(temp.getAge());
            if ("mkyong".equals(temp.getName())) {
                obj.setExtra("this field is for mkyong only!");
            }

            result.add(obj);
        }

        return result;

    }

}

Output

[
    StaffPublic{name='mkyong', age=30, extra='this field is for mkyong only!'}, 
    StaffPublic{name='jack', age=27, extra='null'}, 
    StaffPublic{name='lawrence', age=33, extra='null'}
]

3.3 Java 8 例子.

NowJava8.java

package com.mkyong.java8;

package com.hostingcompass.web.java8;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class NowJava8 {

    public static void main(String[] args) {

        List<staff> staff = Arrays.asList(
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );

        // convert inside the map() method directly.
        List<staffpublic> result = staff.stream().map(temp -&gt; {
            StaffPublic obj = new StaffPublic();
            obj.setName(temp.getName());
            obj.setAge(temp.getAge());
            if ("mkyong".equals(temp.getName())) {
                obj.setExtra("this field is for mkyong only!");
            }
            return obj;
        }).collect(Collectors.toList());

        System.out.println(result);

    }

}

Output

[
    StaffPublic{name='mkyong', age=30, extra='this field is for mkyong only!'}, 
    StaffPublic{name='jack', age=27, extra='null'}, 
    StaffPublic{name='lawrence', age=33, extra='null'}
]

参考

  1. Processing Data with Java SE 8 Streams, Part 1
  2. Java 8 – Filter a Map examples
  3. Java 8 flatMap example
  4. [Collectors JavaDoc][]

[Collectors JavaDoc]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html

如果觉得JSRUN的教程对您有用,请随意打赏。您的支持将鼓励JSRUN继续提供更好的服务

捐助JSRUN
JSRUN前端教程共编系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的本前端教程,让知识的积累变得统一完整、自成体系。 希望广大工程师能一起参与进共编,让零散在世界各处的知识点找到组织、找到家。
X
支付宝
9.99
请使用支付宝扫码支付, 有效时间 5分0秒
超过有效时间后请立即停止支付。
支付故障请联系QQ:565830900
因10.00元通道已占用,系统建议您支付9.99元
正在生成二维码, 此过程可能需要15秒钟
谢谢支持,我一定会更加努力的