【转】jdk8的特性stream().map() 将对象转换成另一个对象

https://www.cnblogs.com/fengli9998/p/9002377.html

 

https://blog.csdn.net/sanchan/article/details/70753645

java8的optional的使用:

http://www.jdon.com/idea/java/using-optional-effectively-in-java-8.html

http://www.runoob.com/java/java8-optional-class.html

Optional 类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象。

Optional 是个容器:它可以保存类型T的值,或者仅仅保存null。Optional提供很多有用的方法,这样我们就不用显式进行空值检测。

Optional 类的引入很好的解决空指针异常。

类声明

以下是一个 java.util.Optional<T> 类的声明:

public final class Optional<T> extends Object

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

在Java 8中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 main(String[] args) {

 

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

 

        //Before 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]

 

        // Extra, streams apply to any data type.

        List<Integer> num = Arrays.asList(1,2,3,4,5);

        List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList());

        System.out.println(collect1); //[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 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))

        );

 

        //Before Java 8

        List<String> result = new ArrayList<>();

        for (Staff x : staff) {

            result.add(x.getName());

        }

        System.out.println(result); //[mkyong, jack, lawrence]

 

        //Java 8

        List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList());

        System.out.println(collect); //[mkyong, jack, lawrence]

 

    }

 

}

 

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

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 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<>();

 

        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;

 

    }

 

}

 

输出

 

[

    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

2

3

4

5

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 -> {

            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);

 

    }

 

}

 

 

[

    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
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值