-1

I have a arraylist and it contains a 100 employee object and each object contains "Name" and "Salary". We need to find the Max salaried employeee from the Object .Please let me know what is the way .

I thought of implementing compareTo and equals Method is it right , and also using Collections.sort Is it the right way or is there any other way

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
gaurav
  • 2,886
  • 6
  • 24
  • 26

2 Answers2

1

If the language is Java, then either implement the Comparable interface (just compareTo--no need for equals) for the objects in the array list and call Collections.sort(arraylist), or else write a Comparator and call Collections.sort(arraylist, comparator). The latter is more flexible, as it doesn't require your objects to always be sorted by salary.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

You need not do the sorting yourself. Your case is perfect for priority queues. Write a comparator as @Ted suggested and add the data to PriorityQueue - it'll give you the min or max based on the data you want the min/max of - salary in your case. Details in this post: How do I use a PriorityQueue?

Community
  • 1
  • 1
PhD
  • 11,202
  • 14
  • 64
  • 112