如果有多个最小值,可以使用list.indexOf方法来获取第一个最小值的索引。然后,可以使用list.subList方法来获取包含第一个最小值的子列表,然后再次使用list.indexOf方法来获取第二个最小值在子列表中的索引。以此类推,直到找到所有最小值的索引。以下是一个示例代码:\n\njava\nOptional<BigDecimal> minValue = list.stream()\n .min(BigDecimal::compareTo);\nif (!minValue.isPresent()) {\n throw new IllegalArgumentException("List is empty");\n}\n\nBigDecimal min = minValue.get();\nList<BigDecimal> subList = list.subList(0, list.indexOf(min) + 1);\nList<Integer> indexes = new ArrayList<>();\nint index = -1;\nwhile ((index = subList.indexOf(min, index + 1)) != -1) {\n indexes.add(index);\n}\n\nreturn indexes;\n\n\n这样,indexes列表将包含所有最小值的索引。

Java中获取List中所有最小值的索引

原文地址: http://www.cveoy.top/t/topic/qaGc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录