I have a arraylist and it contains a 100 employee object and each object contains "Name" and "Salary". We need to find the Max salaried employeee from the Object .Please let me know what is the way .
I thought of implementing compareTo and equals Method is it right , and also using Collections.sort Is it the right way or is there any other way
If the language is Java, then either implement the Comparable interface (just compareTo--no need for equals) for the objects in the array list and call Collections.sort(arraylist), or else write a Comparator and call Collections.sort(arraylist, comparator). The latter is more flexible, as it doesn't require your objects to always be sorted by salary.
You need not do the sorting yourself. Your case is perfect for priority queues. Write a comparator as #Ted suggested and add the data to PriorityQueue - it'll give you the min or max based on the data you want the min/max of - salary in your case. Details in this post:
How do I use a PriorityQueue?
Related
I'm implementing my own java treeset. I think the underlying data structure is a BST and each node in the tree contains Object type data field. However, I stuck at how to compare two Object type data using natural ordering comparator. Is there a compareTo function that compares two objects and return the value of their natural ordering? I'm also thinking using hashcode as index key for each node and do comparison based on that. But it seems that distinct object might have same hashcode. Any advice is appreciated.
instead of BST consider RBT (Red black tree). look at here for more reference.
Now your MyTreeSet can take two kind of objects.
1. Object of java given wrapper class like String, Integer, Long etc Or
2. Object of your own written custom classes.
If your data structure needs to support case 1 one then order can be done easily based on compareTo method which is implemented by all java given wrapper class. You only need to call compareTo method to know which object is greater, equal or lesser than others based on it's return value 0, negative and positive value.
For case 2, that means your MyTreeSet needs to take object of custom classes also then you should implement Comparable interface to your custom classes and write your comparison algorithm there. For example if you want MyTreeSet should take Employee class object then implement Comparable method in Employee class and write implementation of compareTo method based on how emp1 will be compared to emp2. You may want to sort them based on their employee ids.
Hope it helps you.
Set<Student> ts = new TreeSet<Student>();
for(Student s : studentInfo){
ts.add(s);
}
System.out.println(ts);
I've written this above snippet in one of my case block in order to sort a collection of Student Objects.
My question is: What is the difference between using this approach and using Collections.sort(); method.
The difference is that a TreeSet keeps you data sorted at all times while the Collections.sort() method sorts it when you call the method on your Set.
The time complexity of Collections.sort() is O(n*log(n)) while the TreeSet's add()'s complexity is log(n). If you use the same size of data then the complexity in the TreeSet's case will be the same because you repeat the add operation n times.
So you only have to decide whether you want your Set ordered at all times or just at some point. If there is a case in your code when you don't need sorting then you don't need TreeSet but if you will always need it to be sorted then you should use TreeSet.
Please keep in mind that if you want to sort your Set you have to create a List from it first which might introduce some overhead!
Another caveat: As others mentioned TreeSet can only take 1 Comparator while you can supply different Comparators to Collections.sort(). So it depends on your usage. You should give us more information on your use case in order to give you a thorough answer.
1) A TreeSet like all Set refuses duplicate values.
2) A TreeSet maintains the sort every time you insert elements while a list sorted with Collections.sort() will only be sorted after the call to sort() (and will not maintain this sort upon add()).
3) Collections.sort() allows to sort the list on differents criterias using different Comparators. With a TreeSet, you can also give a Comparator but you will need to instantiate one TreeSet for each Comparator.
I've got Treasure objects in a TreasureCollectionDB class.
The TreasureCollectionDB class has a Map<Long, Treasure> (long being an id generated by the TreasureCollectionDB) called treasures
and a second data collection/list (available treasures).
The thing I need the other Collection or List to do is hold Treasures which I will add/remove through JSP pages. The Treasures in this list should be unique, but sorted alphabetically (if there's no data holder that does this by it self, I will write a sort method).
Anyone know what data holder I should use? Answers on the internet are confusing me as to which is most suitable.
You may use TreeSet, that should give you the desired results.
Set doesn't allow duplicates and Tree maintains sorted order.
The Treasures may implement Comparable interface so that you can sort on the desired field(s).
You would need to create equals and hash code methods and also write a comparator. That comparator you may pass to a TreeSet and use SortedSet interface.
How would you go about maintaining an order of collection business objects
<BO1, BO2, BO3, BO4>
so that when you remove BO2, amd BO4 you get
<BO1, BO3>
and then when you add BO2
<BO1, BO2, BO3>
You have several ways of doing that but it depends of the type of collection you want to use. Obviously, you don't want to maintain the order of insertions but an order based on the type of elements in the list.
So, before saying use this or that, ask yourself the following question:
Can my collection hold duplicate elements?
1) If YES: then you could use an implementation of a List object (ArrayList, LinkedList, etc). But you will need to sort the list after each insertion:
List<MyObj> list = ...
list.add(myObjInstance);
Collections.sort(list);
To avoid having to sort the list on each insertion you could use the TreeList implementation from Apache Commons Collections.
2) If the answer to the previous question is NO. Then use a TreeSet, you won't need sort the collection on each insertion with that implementation.
Be aware that your object elements have to implement the Comparable interface in order to be sortable.
Unless you use a sorted order, I don't see how the collection is supposed to know that BO2 should go in the middle.
This will do what you want if your Business object implement Comparable
SortedSet<BusObj> bos = new TreeSet<>();
bos.addAll(Arrays.asList(bo1, bo2, bo3, bo4));
bos.removeAll(Arrays.asList(bo2, bo4));
bos.add(bo2);
Make your business object Comparable and use a sorted collection (like TreeSet which is a SortedSet).
Use a SortedSet
http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html
There are 2 options: use a List and do the sorting yourself by inserting at the proper location or use a sorted collection.
The sorted collection I think you want is SortedSet http://docs.oracle.com/javase/6/docs/api/java/util/SortedSet.html.
The SortedSet requires entries to implement the Comparable interface.
There is also another question that you should look at: Sorted collection in Java
Aswering my question:
I guess also PriorityQueue would be a solution, if one were not interested in the random access.
in which situations we have to implement the Comparable interface?
When you want to be able to compare 2 objects and get a result of equal, less than, or greater than.
Implementing Comparable gives your objects a compareTo method. If you add them to a sorted list then they will automatically be sorted based on what your compareTo method returns.
It's pretty basic. I don't know what else there is to add.
When your class implement the Comparable interface, you have to implement the compareTo() method in a way that you can clearly tell where an instance of your class would go in an ordered list of such instances.
Implementing efficient sorting algorithms and ordered collections isn't trivial. Therefore you would do this when you want objects of a class to have natural ordering, so you can use the proven sorting and order-dependent algorithms and classes provided by Java instead of implementing your own, like having the contents of a TreeSet remain ordered after insertions/deletions, or using Collections.sort().