Reputation: 470
I have two classes, DataClass and MY GUI Class.
This is in my GUI Class
final ArrayList<DataClass> MarksData = new ArrayList<DataClass>();
I store 4 elements at a time [Mark1,Mark2,Mark3,Mark4]
How can I find all the maximum mark for Mark1
I tried Object obj = Collections.min()
but doesn't work since it's an arrayList
of type DataClass
.
Any help is appreciated :)
Upvotes: 1
Views: 185
Reputation: 533520
To use Collections.min() you must either make DataClass implements Comparable<DataClass>
or you must provide a custom Comparator<DataClass>
Upvotes: 2
Reputation: 72284
The best way to do this would probably be to use Collections.max()
, but to do this you need to implement Comparable<DataClass>
on the DataClass
class.
This interface lets you define a compareTo(DataClass o)
method where you can write the logic in that method to determine if it's less or greater than the other mark, and then Collections.max()
will take care of finding the maximum for you.
As noted in the comment, you can also write a custom comparator and use the variant of Collections.max()
which takes that custom comparator - but the logic you write to compare the marks is the same.
Note that if you want to find the minimum mark instead, go for Collections.min()
. All other details remain the same.
Upvotes: 5