Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-08
15
Medium Priority
?
254 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:haneef_nb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35069722
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35069867
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 35070176
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:haneef_nb
ID: 35070345
Hi CHEJ,

Thanks a lot..

hats of to your contribution, your example is so helpful to me..
0
 

Author Comment

by:haneef_nb
ID: 35070998
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35072665
Comparable would be used if the general behaviour of an Employee object was to order itself  based on salary. Is that the case?
0
 
LVL 92

Expert Comment

by:objects
ID: 35074779
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
 
LVL 92

Expert Comment

by:objects
ID: 35074787
> 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
 
LVL 92

Expert Comment

by:objects
ID: 35074795
> 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
 

Author Comment

by:haneef_nb
ID: 35075351
Hi Objects,

could you please tell me what is mean by natural ordering of the class.
0
 

Author Comment

by:haneef_nb
ID: 35075368
Hi Objects,

Could please explain in layman words.
0
 
LVL 92

Expert Comment

by:objects
ID: 35075409
theres a great tutorial covering natural ordering here
http://download.oracle.com/javase/tutorial/collections/interfaces/order.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35075463
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
 

Author Closing Comment

by:haneef_nb
ID: 35206982
Thanks..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35207131
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
A solution for Fortify Path Manipulation.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question