22

What I want to achieve is to get min/max attribute value of object from ArrayList<Object>. For example if Object has attribute weight(float), I want heaviest object from the list.

I've tried to implement Comparable to get max/min value but this returns same value for min and same for max for some reason. (I don't know if it works with floats)

    val maxVal: Float = arrayList.max().floatVal1
    val minVal: Float = arrayList.min().floatVal1

    data class CustomObject(var val1: String, var floatVal1: Float , var floatVal2: Float?, var floatVal3: Float?, var floatVal4: Float?): Comparable<CustomObject>{
        override fun compareTo(other: CustomObject) = (floatVal1 - other.floatVal1).toInt()
    }

That specific question from duplicate post does not show me how to get max/min value based on Float. That's the problem. If I want to modify Comparator it accepts only Int. And i cant use that stream feature because my app is for API 23+ not 24+

martin1337
  • 2,384
  • 6
  • 38
  • 85
  • Possible duplicate of [Java - getting max value from an arraylist of objects?](https://stackoverflow.com/questions/19338686/java-getting-max-value-from-an-arraylist-of-objects) – jsosnowski Sep 23 '18 at 13:06

4 Answers4

37

I think you're looking for minBy and maxBy:

 val minObject: CustomObject? = arrayList.minBy { it.floatVal1 }
 val maxObject: CustomObject? = arrayList.maxBy { it.floatVal1 }
zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • These give you the `CustomObject` instances that have the smallest/largest `floatVal1` values in the list, is that not what you want? If you want that actual smallest/largest value, you can read it from these objects as well. – zsmb13 Sep 23 '18 at 13:31
21
val maxObj: Object? = arrayList.maxByOrNull { it.floatVal1 }
val minObj: Object? = arrayList.minByOrNull { it.floatVal2 }

maxBy, minBy are deprecated since Kotlin 1.4

ajw
  • 2,568
  • 23
  • 27
  • 1
    What's the benefit of using `maxByOrNull` over `minBy` as suggested by [the accepted answer](https://stackoverflow.com/a/52466423/3025856)? Can you edit your answer to call out this difference, and explain why or when it might be preferred? Thank you. – Jeremy Caney Oct 16 '20 at 19:26
  • 4
    @JeremyCaney because they are deprecated. Since maxByOrNull, minByOrNull returns a nullable object we can check and avoid crashes. – ajw Oct 19 '20 at 06:45
4

This return non-null types:

val max = list.maxOf { it.value }
val min = list.minOf { it.value }
Falchio
  • 174
  • 1
  • 14
  • 1
    If your integer values are nullable then max or min values need to be checked whether if NaN or not. Ex: if(max.isNan()) – charman Apr 06 '23 at 04:04
1

Below Code returns Max value of your parameter in list of objects.

Ex)100 is max marks obtained all students

list.maxOf { it.yourParameter } // Returns Max value of yourParameter value in list of objects

Below Code returns Object of Max value of your parameter in list of objects.

Ex)Student object who got 100 marks

list.maxBy { it.yourParameter } // Returns Object of Max of yourParameter value in list of objects
Tarun Anchala
  • 2,232
  • 16
  • 15