java8 stream 计算list对象string类型的的和
时间: 2023-04-01 22:04:07 浏览: 133
可以使用stream的reduce方法来计算list对象string类型的和,示例代码如下:
List<String> list = Arrays.asList("1", "2", "3", "4", "5");
String result = list.stream().reduce("", (a, b) -> a + b);
System.out.println(result);
输出结果为:12345
相关问题
java8 stream 计算list对象string类型属性的的和
可以使用stream的mapToInt方法将list中的string类型属性转换为int类型,然后使用sum方法计算它们的和。具体代码如下:
List<String> list = Arrays.asList("1", "2", "3", "4", "5");
int sum = list.stream().mapToInt(Integer::parseInt).sum();
System.out.println(sum); // 输出15
java8 stream 计算list对象集合string类型属性的的和
可以使用stream的mapToInt方法将list中的string类型属性转换为int类型,然后使用sum方法计算总和。示例代码如下:
List<MyObject> list = new ArrayList<>();
int sum = list.stream().mapToInt(obj -> Integer.parseInt(obj.getStringProperty())).sum();
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)