一个List对象转换为另一个List对象,List中存放不同类型对象之间的转换
时间: 2024-05-09 09:18:41 浏览: 36
这个问题需要更具体的说明,因为不同类型对象之间的转换需要针对具体的对象类型进行操作。以下是一些常见的List对象类型之间的转换示例:
1. List<String> 转换为 List<Integer>
```java
List<String> stringList = Arrays.asList("1", "2", "3");
List<Integer> intList = stringList.stream().map(Integer::valueOf).collect(Collectors.toList());
```
2. List<Integer> 转换为 List<String>
```java
List<Integer> intList = Arrays.asList(1, 2, 3);
List<String> stringList = intList.stream().map(String::valueOf).collect(Collectors.toList());
```
3. List<Map<String, Object>> 转换为 List<POJO>
```java
List<Map<String, Object>> mapList = new ArrayList<>();
mapList.add(new HashMap<String, Object>() {{
put("id", 1);
put("name", "John");
}});
mapList.add(new HashMap<String, Object>() {{
put("id", 2);
put("name", "Alice");
}});
List<Person> personList = mapList.stream().map(map -> new Person((int) map.get("id"), (String) map.get("name"))).collect(Collectors.toList());
```
4. List<POJO> 转换为 List<Map<String, Object>>
```java
List<Person> personList = Arrays.asList(new Person(1, "John"), new Person(2, "Alice"));
List<Map<String, Object>> mapList = personList.stream().map(person -> {
Map<String, Object> map = new HashMap<>();
map.put("id", person.getId());
map.put("name", person.getName());
return map;
}).collect(Collectors.toList());
```
5. List<Object> 转换为 List<String>
```java
List<Object> objectList = Arrays.asList(1, "John", true);
List<String> stringList = objectList.stream().map(Object::toString).collect(Collectors.toList());
```
6. List<String> 转换为 List<Object>
```java
List<String> stringList = Arrays.asList("1", "John", "true");
List<Object> objectList = stringList.stream().map(str -> {
if (str.matches("\\d+")) {
return Integer.valueOf(str);
} else if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("false")) {
return Boolean.valueOf(str);
} else {
return str;
}
}).collect(Collectors.toList());
```
注意:以上示例仅供参考,实际应用中需要根据具体情况进行修改和适配。同时还需要注意类型转换可能会导致数据精度或类型错误等问题,需要进行适当的类型检查和异常处理。
相关推荐
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)