Solved

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

Posted on 2011-03-08
15
242 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 500 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 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