How to store the objects in collection in a specified order?

Hi,

I have a java class Employee, in that employee class i have empId,empName,empSal fields, i want to store these employee objects in a ArrayLIst in based on empSal.

Please explain how can i add the employee elements in the ArrayList in the above manner.
haneef_nbAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Since you have unique objects, you can use a TreeSet. No sorting is necessary. Run the following and you'll see it stores in ascending salary order. Sample output:


[id:2|pay:48.91|name:C, id:0|pay:49.96|name:A, id:1|pay:99.35|name:B]
Employee.java.txt
0
 
CEHJCommented:
You're using the wrong type of collection. A List maintains the order in which they were added. You could use a TreeSet with a Comparator on empSal, but it wouldn't allow duplicate salaries. You could sort the List on that field though
0
 
for_yanCommented:
Once you override method compareTo of the Object class for the objects you store in your ArrayList just comparing them by salary, every time you add your new element you can run Collections.sort(list) and you'll have them ordered all the time. Unless you are dealing with very long ArrayList it will not take too much time
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
haneef_nbAuthor Commented:
Hi CHEJ,

Thanks a lot..

hats of to your contribution, your example is so helpful to me..
0
 
haneef_nbAuthor Commented:
Hi CHEJ,

Can you give me the same example  by using Comparable interface, and please tell me when we will use comparator and when will we use Comparable?
0
 
CEHJCommented:
Comparable would be used if the general behaviour of an Employee object was to order itself  based on salary. Is that the case?
0
 
objectsCommented:
you can simply sort your list as for_yan suggested earlier

http://helpdesk.objects.com.au/java/how-to-sort-java-list-using-custom-ordering

Far simpler to deal with lists and in your situation it would appear more appropriate (as you have a *list* of data)
0
 
objectsCommented:
> Can you give me the same example  by using Comparable interface

heres a comparable example
http://helpdesk.objects.com.au/java/how-do-i-use-collections-sort-with-generics
0
 
objectsCommented:
> and please tell me when we will use comparator and when will we use Comparable?

you'd be best off using the Comparator interface. Comparable is for defining the natural ordering of the class
0
 
haneef_nbAuthor Commented:
Hi Objects,

could you please tell me what is mean by natural ordering of the class.
0
 
haneef_nbAuthor Commented:
Hi Objects,

Could please explain in layman words.
0
 
objectsCommented:
theres a great tutorial covering natural ordering here
http://download.oracle.com/javase/tutorial/collections/interfaces/order.html
0
 
CEHJCommented:
Natural ordering is often subjective and depends on the functionality of the class. An Employee natural ordering, would most likely be on the id. That's why a Comparator for salary would be better as i mentioned earlier, since the salary probably wouldn't determine natural order
0
 
haneef_nbAuthor Commented:
Thanks..
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.