wordpress小程序調(diào)用seo關(guān)鍵詞排名優(yōu)化銷(xiāo)售
目錄
方法一:使用傳統(tǒng)循環(huán)
方法二:使用 Java 8 的流(Stream)API
方法三:使用 Guava 庫(kù)
總結(jié)
將 List<Map<String, Object>>
轉(zhuǎn)換為 List<JSONObject>
有多種方法。以下是幾種常見(jiàn)的方法,包括使用傳統(tǒng)的循環(huán)、Java 8 的流(Stream)API 和 Guava 庫(kù)。
方法一:使用傳統(tǒng)循環(huán)
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class Main {public static void main(String[] args) {// 創(chuàng)建一個(gè)示例 List<Map<String, Object>>List<Map<String, Object>> originalList = new ArrayList<>();Map<String, Object> map1 = new HashMap<>();map1.put("name", "John Doe");map1.put("age", 30);map1.put("isStudent", false);Map<String, Object> map2 = new HashMap<>();map2.put("name", "Jane Doe");map2.put("age", 25);map2.put("isStudent", true);originalList.add(map1);originalList.add(map2);// 使用傳統(tǒng)循環(huán)轉(zhuǎn)換為 List<JSONObject>List<JSONObject> convertedList = new ArrayList<>();for (Map<String, Object> map : originalList) {convertedList.add(new JSONObject(map));}// 打印結(jié)果for (JSONObject json : convertedList) {System.out.println(json.toString());}}
}
方法二:使用 Java 8 的流(Stream)API
import org.json.JSONObject;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;public class Main {public static void main(String[] args) {// 創(chuàng)建一個(gè)示例 List<Map<String, Object>>List<Map<String, Object>> originalList = new ArrayList<>();Map<String, Object> map1 = new HashMap<>();map1.put("name", "John Doe");map1.put("age", 30);map1.put("isStudent", false);Map<String, Object> map2 = new HashMap<>();map2.put("name", "Jane Doe");map2.put("age", 25);map2.put("isStudent", true);originalList.add(map1);originalList.add(map2);// 使用 Stream API 轉(zhuǎn)換為 List<JSONObject>List<JSONObject> convertedList = originalList.stream().map(JSONObject::new).collect(Collectors.toList());// 打印結(jié)果for (JSONObject json : convertedList) {System.out.println(json.toString());}}
}
方法三:使用 Guava 庫(kù)
如果你已經(jīng)在項(xiàng)目中使用了 Guava 庫(kù),可以利用 FluentIterable
和 Functions
來(lái)簡(jiǎn)化轉(zhuǎn)換過(guò)程。
首先,確保你已經(jīng)添加了 Guava 庫(kù)的依賴(lài)。如果你使用的是 Maven,可以在 pom.xml
中添加以下依賴(lài):
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>31.0.1-jre</version>
</dependency>
然后,使用 Guava 進(jìn)行轉(zhuǎn)換:
import com.google.common.collect.FluentIterable;
import com.google.common.base.Functions;
import org.json.JSONObject;
import java.util.List;
import java.util.Map;public class Main {public static void main(String[] args) {// 創(chuàng)建一個(gè)示例 List<Map<String, Object>>List<Map<String, Object>> originalList = new ArrayList<>();Map<String, Object> map1 = new HashMap<>();map1.put("name", "John Doe");map1.put("age", 30);map1.put("isStudent", false);Map<String, Object> map2 = new HashMap<>();map2.put("name", "Jane Doe");map2.put("age", 25);map2.put("isStudent", true);originalList.add(map1);originalList.add(map2);// 使用 Guava 轉(zhuǎn)換為 List<JSONObject>List<JSONObject> convertedList = FluentIterable.from(originalList).transform(Functions.<Map<String, Object>>identity().compose(JSONObject::new)).toList();// 打印結(jié)果for (JSONObject json : convertedList) {System.out.println(json.toString());}}
}
總結(jié)
-
方法一:使用傳統(tǒng)循環(huán)
- 創(chuàng)建一個(gè)空的?
List<JSONObject>
。 - 遍歷?
List<Map<String, Object>>
,將每個(gè)?Map
?轉(zhuǎn)換為?JSONObject
?并添加到新的列表中。
- 創(chuàng)建一個(gè)空的?
-
方法二:使用 Java 8 的流(Stream)API
- 使用?
stream()
?方法創(chuàng)建一個(gè)流。 - 使用?
map(JSONObject::new)
?將每個(gè)?Map
?轉(zhuǎn)換為?JSONObject
。 - 使用?
collect(Collectors.toList())
?將流中的元素收集到一個(gè)新的列表中。
- 使用?
-
方法三:使用 Guava 庫(kù)
- 使用?
FluentIterable.from(originalList)
?創(chuàng)建一個(gè)可變的迭代器。 - 使用?
transform
?方法將每個(gè)?Map
?轉(zhuǎn)換為?JSONObject
。 - 使用?
toList()
?方法將結(jié)果收集到一個(gè)新的列表中。
- 使用?