Java 如何从列表中收集多个最大值
如何从具有多个max的Java 如何从列表中收集多个最大值,java,list,max,Java,List,Max,如何从具有多个max的ArrayList中获取max?例如,如果一个arraylist包含存储在索引2、3和6中的max=20,您如何获得所有指示?通常的max finder只存储最大met值,在这里您必须维护一个与最大值匹配的索引列表。显而易见的方法是首先通过,然后收集项目等于最大值的标记: public <T extends Comparable<? super T>> List<Integer> maxIndicies(List<T> inp
ArrayList
中获取max?例如,如果一个arraylist
包含存储在索引2、3和6中的max=20,您如何获得所有指示?通常的max finder只存储最大met值,在这里您必须维护一个与最大值匹配的索引列表。显而易见的方法是首先通过,然后收集项目等于最大值的标记:
public <T extends Comparable<? super T>> List<Integer> maxIndicies(List<T> input) {
if (input.isEmpty()) // avoid exception thrown by Collections.max() if input is empty
return Collections.emptyList();
final T max = Collections.max(input);
return IntStream.range(0, input.size())
.filter(i -> input.get(i).compareTo(max) == 0)
.boxed()
.collect(Collectors.toList());
}
这不会为您提供max item本身的值,但您可以通过任何返回的索引获得它,如下所示:
List<Integer> maxInd = maxIndicies(list);
maxValue = maxInd.isEmpty() ? null : list.get(maxInd.get(0));
List maxInd=maxIndicies(列表);
maxValue=maxInd.isEmpty()?null:list.get(maxInd.get(0));
您可以通过以下方式完成:
public void findMaxIndices() {
//Your list with numbers
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9));
//Sorted Map which will contain key as numbers and value as list of indices where your 'key' exists in the list
SortedMap<Integer, List<Integer>> indexMapping = new TreeMap<Integer, List<Integer>>();
for(int i = 0; i< list.size(); i++) {
//Get the number at index i
int number = list.get(i);
//Check if any index corresponding to 'number' as index has been added to your map
List<Integer> mapping = indexMapping.get(number);
if(mapping == null) {
//instantiate the list if no index has been added yet
mapping = new ArrayList<Integer>();
//Key as your 'number'
indexMapping.put(number, mapping);
}
//Add the index of the 'number' to the mapping list, which is mapped by key as 'number'
mapping.add(i);
}
//Last key in sorted map will be your highest number in the list, get the value corresponding to it. Following prints: [8,17]
int maxNumber = indexMapping.lastKey(); //Maximum number found
System.out.println(indexMapping.get(maxNumber)); //Indices where maximum number exists
}
这听起来像是编程课程的家庭作业。你应该自己做,但不管怎样,这里是解决办法
private List<Integer> getAllMaxIndices(List<Integer> aList) {
List<Integer> result = new ArrayList<Integer>();
// check argument
if (aList == null || aList.isEmpty()) {
return result;
}
// initialize the list with the index of the first element
result.add(0);
Integer tmpInt;
Integer tmpFirstIndexOfMaxInt;
Integer tmpMaxInt;
for (int i = 0; i < aList.size(); i++) {
// save the current integer and the currently maximum integer
tmpInt = aList.get(i);
tmpFirstIndexOfMaxInt = result.get(0);
tmpMaxInt = aList.get(tmpFirstIndexOfMaxInt);
// if the current element is greater than the last found
if (tmpInt > tmpMaxInt) {
// empty the result
result.clear();
// start collecting indices again
result.add(i);
}
// if the current element is equal to the last found
else if (tmpInt.intValue() == tmpMaxInt.intValue()) {
// insert the current index in the result
result.add(i);
}
}
return result;
}
私有列表GetAllMaxIndex(列表列表列表){
列表结果=新建ArrayList();
//检查参数
if(aList==null | | aList.isEmpty()){
返回结果;
}
//使用第一个元素的索引初始化列表
结果:添加(0);
整数tmpInt;
整数tmpFirstIndexOfMaxInt;
整数tmpMaxInt;
对于(int i=0;itmpMaxInt){
//清空结果
result.clear();
//再次开始收集索引
结果.添加(i);
}
//如果当前元素等于上次找到的元素
else if(tmpInt.intValue()==tmpMaxInt.intValue()){
//在结果中插入当前索引
结果.添加(i);
}
}
返回结果;
}
我将留给您编写测试此函数的代码。使用流的另一种方法。该解决方案假设您想知道最大值出现的频率(而不是索引)
public static Map.Entry getMaxWithOccessions(
(列表){
返回列表
.stream()
.收集(
Collectors.groupingBy(i->i,TreeMap::new,
Collectors.counting()).lastEntry();
}
我会使用一个简单易读的for循环
public List<Integer> getMaxIndices(List<Integer> values) {
Integer max = Collections.max(values);
List<Integer> maxIndices = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
if (values.get(i).equals(max)) {
maxIndices.add(Integer.valueOf(i));
}
}
return maxIndices;
}
public List getMaxIndexes(列表值){
整数max=Collections.max(值);
List maxindex=new ArrayList();
对于(int i=0;i
整数maxValue=Collections.max(列表)
int numberofMax=Collections.frequency(列表,最大值)
此“numberofMax”将返回“列表”的最大值 返回一个
列表
而不是int
。欢迎使用StackOverflow,在执行此操作之前,您可能需要阅读。给出一点上下文来解释到目前为止你尝试了什么以及为什么它不按它的工作(即:错误消息),也考虑花时间来格式化你的问题以便于读者理解。首先对项目进行迭代以找到最大值,然后对项目进行迭代以收集标识符,其中“代码>列表。获取(i)。您可以将max
初始化为input[1]
并从1
开始迭代。
private List<Integer> getAllMaxIndices(List<Integer> aList) {
List<Integer> result = new ArrayList<Integer>();
// check argument
if (aList == null || aList.isEmpty()) {
return result;
}
// initialize the list with the index of the first element
result.add(0);
Integer tmpInt;
Integer tmpFirstIndexOfMaxInt;
Integer tmpMaxInt;
for (int i = 0; i < aList.size(); i++) {
// save the current integer and the currently maximum integer
tmpInt = aList.get(i);
tmpFirstIndexOfMaxInt = result.get(0);
tmpMaxInt = aList.get(tmpFirstIndexOfMaxInt);
// if the current element is greater than the last found
if (tmpInt > tmpMaxInt) {
// empty the result
result.clear();
// start collecting indices again
result.add(i);
}
// if the current element is equal to the last found
else if (tmpInt.intValue() == tmpMaxInt.intValue()) {
// insert the current index in the result
result.add(i);
}
}
return result;
}
public static Map.Entry<Integer, Long> getMaxWithOccurrences(
List<Integer> list) {
return list
.stream()
.collect(
Collectors.groupingBy(i -> i, TreeMap::new,
Collectors.counting())).lastEntry();
}
public List<Integer> getMaxIndices(List<Integer> values) {
Integer max = Collections.max(values);
List<Integer> maxIndices = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
if (values.get(i).equals(max)) {
maxIndices.add(Integer.valueOf(i));
}
}
return maxIndices;
}