轉(zhuǎn):
https://blog.csdn.net/sanchan/article/details/70753645;
https://blog.csdn.net/u010425776/article/details/52344425
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组哩,調(diào)用get()方法會返回該對象。
Optional 是個容器:它可以保存類型T的值,或者僅僅保存null糯而。Optional提供很多有用的方法,這樣我們就不用顯式進行空值檢測泊窘。
Optional 類的引入很好的解決空指針異常熄驼。
類聲明
以下是一個java.util.Optional<T>類的聲明:
public?final?class?Optional?extends?Object
在Java 8中stream().map(),您可以將對象轉(zhuǎn)換為其他對象烘豹。查看以下示例:
1.大寫字符串列表
1.1簡單的Java示例將Strings列表轉(zhuǎn)換為大寫瓜贾。
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 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 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對象列表轉(zhuǎn)換為對象列表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 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'}
]
過濾
List<Globalconst> datas = GlobalconstCache.getGlobalconstList("TABusinessType");
? ? ? ? List<Globalconst> result = datas
? ? ? ? ? ? .stream()
? ? ? ? ? ? .filter(
? ? ? ? ? ? ? ? (globalconst) -> globalconst.getInteriorid().equals("A")
? ? ? ? ? ? ? ? ? ? || globalconst.getInteriorid().equals("S")
? ? ? ? ? ? ? ? ? ? || globalconst.getInteriorid().equals("R"))
? ? ? ? ? ? .collect(Collectors.toList());
排序
List<ReckoningInfo> orderList=list.stream().sorted(Comparator.comparing(ReckoningInfo::getTradeDate)).collect(Collectors.toList());