Solved

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

Posted on 2011-03-08
15
219 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now