Solved

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

Posted on 2011-03-08
15
231 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
  • 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 40
Configure a Bean in an XML file 4 42
hashmap order 17 37
Select statement in @Query (JPA Repository) - SpringMVC 2 16
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…
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…
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.

828 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