开发者

Collections -ArrayList

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开发者_如何学JAVA 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


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜