Java 8中Streams之map()功能及示例说明

乔欣 Java经验 发布时间:2023-01-30 22:40:56 阅读数:15756 1
下文笔者讲述java8中map()方法的功能及示例说明,如下所示

Stream之map()功能

stream().map():
     可将对象转换为其他对象
例:字符串中所有字符串转换为大写
package com.java265.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<>();
        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] 
    }
}

对象列表->字符串列表

将staff对象列表中获取所有name值
 
package com.java265.java8;
import java.math.BigDecimal;
public class Staff {
 
    private String name;
    private int age;
    private BigDecimal salary;
	//...
}

package com.java265.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("java265", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("maomao", 33, new BigDecimal(30000))
        );
 
        //Java 8以前的写法
        List<String> result = new ArrayList<>();
        for (Staff x : staff) {
            result.add(x.getName());
        }
        System.out.println(result); //[java265, jack, maomao]
 
        //Java 8的写法
        List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList());
        System.out.println(collect); //[java265, jack, maomao]
 
    }
}

对象列表->其她对象列表

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


package com.java265.java8;
 
public class StaffPublic {
 
    private String name;
    private int age;
    private String extra;
    //...
}
  

//java8 stram写法
package com.java265.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("java265", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("maomao", 33, new BigDecimal(30000))
        );
 
		// convert inside the map() method directly.
        List<StaffPublic> result = staff.stream().map(temp -> {
            StaffPublic obj = new StaffPublic();
            obj.setName(temp.getName());
            obj.setAge(temp.getAge());
            if ("java265".equals(temp.getName())) {
                obj.setExtra("this field is for java265 only!");
            }
            return obj;
        }).collect(Collectors.toList());
 
        System.out.println(result);
 
    }
	
}

-----运行以上代码,将输出以下信息-----
[
	StaffPublic{name='java265', age=30, extra='this field is for java265 only!'}, 
	StaffPublic{name='jack', age=27, extra='null'}, 
	StaffPublic{name='maomao', age=33, extra='null'}
]
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202301/16750902005585.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者