集合 stream流最大值
时间: 2023-08-24 22:13:55 浏览: 103
使用stream流求集合中的最大值可以通过以下代码实现:
List<Integer> list = Arrays.asList(new Integer\[\]{1, 2, 3, 4, 5, 6, 78, 9});
int max = list.stream().max(Integer::compareTo).get();
这段代码首先将数组转化为集合,然后使用stream流的max方法结合Integer的compareTo方法来获取集合中的最大值。最后通过get方法获取最大值的结果。\[2\]
#### 引用[.reference_title]
- *1* *3* [java中使用stream流求数组元素的最大值最小值以及元素的和](https://blog.csdn.net/qq_54650406/article/details/124646449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [JAVA stream流之reduce求最大值、最小值](https://blog.csdn.net/qq_41844287/article/details/126365266)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文