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 🙂
The best way to do this would probably be to use
Collections.max(), but to do this you need to implementComparable<DataClass>on theDataClassclass.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 thenCollections.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.