Java 8 Streams map()
在java8中, stream().map()可以讓你轉化一個物件成其他的物件。下面我們通過下面的例子來看一下:
- String集合字串轉為大寫
TestJava8.java
package com.ricky.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]
}
}
- List of objects -> List of String
- 從staff物件集合中獲取所有的名字。
Staff.java
package com.ricky.java8;
import java.math.BigDecimal;
public class Staff {
private String name;
private int age;
private BigDecimal salary;
//...
}
TestJava8.java
package com.ricky.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("ricky", 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); //[ricky, jack, lawrence]
//Java 8
List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList());
System.out.println(collect); //[ricky, jack, lawrence]
}
}
- List of objects -> List of other objects
一個例子展示怎麼轉化staff物件為StaffPublic物件
Staff.java
package com.ricky.java8;
import java.math.BigDecimal;
public class Staff {
private String name;
private int age;
private BigDecimal salary;
//...
}
StaffPublic.java
package com.ricky.java8;
public class StaffPublic {
private String name;
private int age;
private String extra;
//...
}
- java8以前的方式
BeforeJava8.java
package com.ricky.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("ricky", 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 ("ricky".equals(temp.getName())) {
obj.setExtra("this field is for ricky only!");
}
result.add(obj);
}
return result;
}
}
輸出
[
StaffPublic{name='ricky', age=30, extra='this field is for ricky only!'},
StaffPublic{name='jack', age=27, extra='null'},
StaffPublic{name='lawrence', age=33, extra='null'}
]
- java8的方式
NowJava8.java
package com.ricky.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("ricky", 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 ("ricky".equals(temp.getName())) {
obj.setExtra("this field is for ricky only!");
}
return obj;
}).collect(Collectors.toList());
System.out.println(result);
}
}
輸出
[
StaffPublic{name='ricky', age=30, extra='this field is for ricky only!'},
StaffPublic{name='jack', age=27, extra='null'},
StaffPublic{name='lawrence', age=33, extra='null'}
]
翻譯
ricky 20170730
交流群:244930845
相關文章
- Java 8 Streams filterJavaFilter
- 8個實用的Java Streams APIJavaAPI
- Java 8 Streams 中的資料庫 CRUD 操作Java資料庫
- Java 8 Streams API:對Stream分組和分割槽JavaAPI
- list轉map,使用java8,stream流Java
- STREAMS筆記(8) rule - 自定義筆記
- java8 實現map以value值排序Java排序
- Java Streams 的潛力Java
- Java 8 中 Map 騷操作之 merge() 的用法Java
- 如何在Java 8中將List轉換為Map?Java
- java mapJava
- 你只會用 map.put?試試 Java 8 compute ,操作 Map 更輕鬆!Java
- java .stream(). 使用介紹 Streams APIJavaAPI
- Java 中的map - The Map Interface.Java
- Java8 中使用Stream 讓List 轉 Map使用總結Java
- Java8 中 List 轉 Map(Collectors.toMap) 使用技巧Java
- 聊一聊Java8中map的putIfAbsent,computeIfAbsent 方法Java
- Java集合類——MapJava
- JAVA集合——Map介面Java
- Java筆記——【Map】Java筆記
- Java List/Set/MapJava
- java Map及Map.Entry詳解Java
- Go 基礎教程--8-mapGo
- java中Map根據Map的value取keyJava
- Java春招面試複習:有關於Java Map,應該掌握的8個問題Java面試
- JAVA集合框架 - Map介面Java框架
- Java map轉JSONJavaJSON
- Java Map集合練習Java
- 【java】【Map】HashMap、Hashtable、CollectionsJavaHashMap
- java Map Set遍歷Java
- java中的Map集合Java
- 使用Java和Reactive Streams構建流式應用JavaReact
- Oracle StreamsOracle
- STREAMS筆記(2) 其他建立Streams的方式筆記
- java Map相關總結Java
- Java物件轉換成MapJava物件
- Java集合四:Map簡介;Java
- 深入淺出java的MapJava