jdk8 新特性stream.map

1.大写字符串列表

1.1 简单的Java示例将Strings列表转换为大写

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 mainString[] args) {
 
        List<String> alpha = Arrays.asList"a", "b", "c", "d");
 
        //Before Java8
        List<String> alphaUpper = new ArrayList<>);
        for String s : alpha) {
            alphaUpper.adds.toUpperCase));
        }
 
        System.out.printlnalpha); //[a, b, c, d]
        System.out.printlnalphaUpper); //[A, B, C, D]
 
        // Java 8
        List<String> collect = alpha.stream).mapString::toUpperCase).collectCollectors.toList));
        System.out.printlncollect); //[A, B, C, D]
 
        // Extra, streams apply to any data type.
        List<Integer> num = Arrays.asList1,2,3,4,5);
        List<Integer> collect1 = num.stream).mapn -> n * 2).collectCollectors.toList));
        System.out.printlncollect1); //[2, 4, 6, 8, 10]
 
    }
 
}

2.对象列表 - >字符串列表

2.1 name从staff对象列表中获取所有值

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 mainString[] args) {
 
        List<Staff> staff = Arrays.asList
                new Staff"mkyong", 30, new BigDecimal10000)),
                new Staff"jack", 27, new BigDecimal20000)),
                new Staff"lawrence", 33, new BigDecimal30000))
        );
 
        //Before Java 8
        List<String> result = new ArrayList<>);
        for Staff x : staff) {
            result.addx.getName));
        }
        System.out.printlnresult); //[mkyong, jack, lawrence]
 
        //Java 8
        List<String> collect = staff.stream).mapx -> x.getName)).collectCollectors.toList));
        System.out.printlncollect); //[mkyong, jack, lawrence]
}

3. 对象列表 - >其他对象列表

3.1 此示例说明如何将staff对象列表转换为对象列表StaffPublic

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 mainString[] args) {
 
        List<Staff> staff = Arrays.asList
                new Staff"mkyong", 30, new BigDecimal10000)),
                new Staff"jack", 27, new BigDecimal20000)),
                new Staff"lawrence", 33, new BigDecimal30000))
        );
 
        List<StaffPublic> result = convertToStaffPublicstaff);
        System.out.printlnresult);
 
    }
 
    private static List<StaffPublic> convertToStaffPublicList<Staff> staff) {
 
        List<StaffPublic> result = new ArrayList<>);
 
        for Staff temp : staff) {
 
            StaffPublic obj = new StaffPublic);
            obj.setNametemp.getName));
            obj.setAgetemp.getAge));
            if "mkyong".equalstemp.getName))) {
                obj.setExtra"this field is for mkyong only!");
            }
 
            result.addobj);
        }
 
        return result;
 
    }
 
}

输出:

[
    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 mainString[] args) {
 
        List<Staff> staff = Arrays.asList
                new Staff"mkyong", 30, new BigDecimal10000)),
                new Staff"jack", 27, new BigDecimal20000)),
                new Staff"lawrence", 33, new BigDecimal30000))
        );
 
        // convert inside the map) method directly.
        List<StaffPublic> result = staff.stream).maptemp -> {
            StaffPublic obj = new StaffPublic);
            obj.setNametemp.getName));
            obj.setAgetemp.getAge));
            if "mkyong".equalstemp.getName))) {
                obj.setExtra"this field is for mkyong only!");
            }
            return obj;
        }).collectCollectors.toList));
 
        System.out.printlnresult);
 
    }
 
}

输出:

[
    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'}
]

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注