How to get maximum value from the Collection (for example ArrayList)?
You can use the Collections API
to achieve what you want easily - read efficiently - enough
Javadoc for Collections.max
Collections.max(arrayList);
Returns the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface.
How to get maximum value from of the first N values of an ArrayList?
List
has a subList method that you can use:
Collections.max(arrayList.subList(0, 3))
There is no subList
for Collection
s in general, as not all collections are lists and, for some, the "first N elements" doesn't make a lot of sense, as they don't maintain any meaningful order (e.g. HashSet
).
You can take the first three elements of any Collection
(in whatever order the collection will provide), by iterating through it with a limit. It's probably best to use a stream for this:
yourCollection.stream().limit(3).collect(Collectors.toList());
Or you can find what you're looking for directly on the stream, without collecting the elements in some collection:
Optional<Integer> max = yourCollection.stream()
.limit(3)
.max(Comparator.naturalOrder());
Getting max value from an arraylist of objects?
Use a Comparator
with Collections.max()
to let it know which is greater in comparison.
Also See
- How to use custom
Comparator
How to find the max value of an Arraylist and two of its Index position using Java
Untested:
public static int maxIndex(List<Integer> list) {
Integer i=0, maxIndex=-1, max=null;
for (Integer x : list) {
if ((x!=null) && ((max==null) || (x>max))) {
max = x;
maxIndex = i;
}
i++;
}
return maxIndex
}
// ...
maxIndex(Arrays.asList(1, 2, 3, 2, 1)); // => 2
maxIndex(Arrays.asList(null, null)); // => -1
maxIndex(new ArrayList<Integer>()); // => -1
How to loop to find maximum value in a Arraylist
You have to iterate over your array which holds the days, find the highest count and return the index of that value.
public int mostPopularDay() { //finds the most popular Day of the year
int popularDay = 0;
int max = -1;
for(int i = 0; i < birthDayStats.length; i++) {
if(birthDayStats[i] > max) {
max = birthDayStats[i];
popularDay = i;
}
}
return popularDay + 1; //Adding +1 since there is no 0th day of the month
}
Keep in mind, that this does only return the most popular day for birthdays, not the most popular date. But I assume that this is what you wanted.
How to get maximum value in an ArrayList
If you just want max value then use this:
public int getMax(ArrayList list){
int max = Integer.MIN_VALUE;
for(int i=0; i<list.size(); i++){
if(list.get(i) > max){
max = list.get(i);
}
}
return max;
}
and more good way is comparator:
public class compareProduct implements Comparator<Product> {
public int compare(Product a, Product b) {
if (a.getPrice() > b.getPrice())
return -1; // highest value first
if (a.getPrice() == b.getPrice())
return 0;
return 1;
}
}
and then just do this:
Product p = Collections.max(products, new compareProduct());
max value from arraylist part?
You can do something like the code I show below. You iterate the first array, calculating the maximum and updating the second array accordingly. Note that you need an extra array to store the intervals.
private void m1(List<Integer> array1, List<Integer> array2, int range) {
Integer[] rangeArray = new Integer[range];
//Iterate first array
for(int i = 0; i < array1.size(); i++) {
Integer x = array1.get(i);
rangeArray[i%range] = x;
//Update second array
if(i < range - 1) {
array2.add(null);
}
else {
int max = Collections.max(Arrays.asList(rangeArray));
array2.add(max);
}
}
}
Kotlin - Get Maximum value in ArrayList of Ints
You can use built-in functionality with maxOrNull
(docs):
val amplitudes = listOf(1,2,3,4,3,2,1)
val max = amplitudes.maxOrNull() ?: 0
Related Topics
Does a Tcp Socket Connection Have a "Keep Alive"
When Will a String Be Garbage Collected in Java
Java Error: Only a Type Can Be Imported. Xyz Resolves to a Package
Why Were Applets Deprecated in Jdk 9
Confusion: @Notnull VS. @Column(Nullable = False) with JPA and Hibernate
How to Check If Two Words Are Anagrams
Why Does the Ternary Operator Unexpectedly Cast Integers
Reliable File.Renameto() Alternative on Windows
How to Implement a Db Listener in Java
Get Unicode Value of a Character
Simple Division in Java - Is This a Bug or a Feature
Jvm Takes a Long Time to Resolve Ip-Address for Localhost
How Is an Instance Initializer Different from a Constructor
How to Set the System Time in Java
Warning - Build Path Specifies Execution Environment J2Se-1.4