jdk8的特性stream.map

时间:2025-04-01 10:23:10

在Java 8中stream().map(),您可以将对象转换为其他对象

1.大写字符串列表


 
package .java8;
 
import ;
import ;
import ;
import ;
 
public class TestJava8 {
 
    public static void main(String[] args) {
 
        List<String> alpha = ("a", "b", "c", "d");
 
        //Before Java8
        List<String> alphaUpper = new ArrayList<>();
        for (String s : alpha) {
            (());
        }
 
        (alpha); //[a, b, c, d]
        (alphaUpper); //[A, B, C, D]
 
        // Java 8
        List<String> collect = ().map(String::toUpperCase).collect(());
        (collect); //[A, B, C, D]
 
        // Extra, streams apply to any data type.
        List<Integer> num = (1,2,3,4,5);
        List<Integer> collect1 = ().map(n -> n * 2).collect(());
        (collect1); //[2, 4, 6, 8, 10]
 
    }
 
}

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

package .java8;
 
import ;
 
public class Staff {
 
    private String name;
    private int age;
    private BigDecimal salary;
    //...
}
 

 
package .java8;
 
import ;
import ;
import ;
import ;
import ;
 
public class TestJava8 {
 
    public static void main(String[] args) {
 
        List<Staff> staff = (
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );
 
        //Before Java 8
        List<String> result = new ArrayList<>();
        for (Staff x : staff) {
            (());
        }
        (result); //[mkyong, jack, lawrence]
 
        //Java 8
        List<String> collect = ().map(x -> ()).collect(());
        (collect); //[mkyong, jack, lawrence]
 
    }
 
}
 

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

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

package .java8;
 
import ;
 
public class Staff {
 
    private String name;
    private int age;
    private BigDecimal salary;
    //...
}
 

 
package .java8;
 
public class StaffPublic {
 
    private String name;
    private int age;
    private String extra;
    //...
}
 
3.2 Java 8之前。
 

 
package .java8;
 
import ;
import ;
import ;
import ;
 
public class BeforeJava8 {
 
    public static void main(String[] args) {
 
        List<Staff> staff = (
                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);
        (result);
 
    }
 
    private static List<StaffPublic> convertToStaffPublic(List<Staff> staff) {
 
        List<StaffPublic> result = new ArrayList<>();
 
        for (Staff temp : staff) {
 
            StaffPublic obj = new StaffPublic();
            (());
            (());
            if ("mkyong".equals(())) {
                ("this field is for mkyong only!");
            }
 
            (obj);
        }
 
        return result;
 
    }
 
}

Java 8的例子

package .java8;
 
package .java8;
 
import ;
import ;
import ;
import ;
 
public class NowJava8 {
 
    public static void main(String[] args) {
 
        List<Staff> staff = (
                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 = ().map(temp -> {
            StaffPublic obj = new StaffPublic();
            (());
            (());
            if ("mkyong".equals(())) {
                ("this field is for mkyong only!");
            }
            return obj;
        }).collect(());
 
        (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'}
]