Before trying to optimise anything you need to ascertain what section/line of code needs optimising and the only way I know of doing that is to use a profiler.
BTW Profilers are freely available in Netbeans and Eclipse.
Once you have identified where the bottleneck is we may then be able to advise on alternative ways of writing the code.
Having said all of that Vectors methods are synchronized so unless the collection will be accessed by multiple threads you will get a small speed improvement by changing to an ArrayList but I doubt it will solve your performance problems.
1) The areMasterViewsEqual() method turns into a compare() method
2) It returns 0 when the views are equal,
3) Has some way of determining when one view should be 'before' another. The determination could be arbitrary, but needs to be consistent (so if compare(part1, part2) returns a positive number, compare(part2, part1) needs to return a negative number).
Firstly thanks for your quick response and secondly I agree I could have explained better what the for loops do right at the start of my thread .Anyways I will do that now
The intention of this nested loop is quite close to what Steve's thinking [Steve thanks for the Comparator suggestion] !
The first for loop contains 60,000 part objects which have master and view as its attributes. What I want to do in the 2nd for loop is to compare the 1st.....59,000 parts attributes from the 1st loop in the list with the 2nd,3rd...60,000 parts attributes in the subsequent iterations of the 2nd loop.
So if the 1st...n part's "master and view" references matches with the 2nd....n parts "master and view" references I need to group all such parts together in a Vector /ArrayList which will then be used further for a service [DB ] invocation outside the 2nd for loop [Updated code snippet with this modification].
This nested for loop takes around 12 minutes to execute in my system [windows 7, RAM 16 GB].
So the goal of the nested for loop is to group together similar parts!
I hope that clarifies things !
So can you help me decide which approach would reduce this performance issue ?
Thanks in advance for your help !
Amit Patole wrote:I will start on steve's suggestion of using Collections.sort(List,Comparator) to further refine the performance hit.
Please let me know if I am in the incorrect direction !
A slight generalization of Ivan's post:
1. If you can come up with some way of determining whether two of your objects are "equal", you can write an equals() method for it.
2. If you write an equals() method, you should also write a hashCode() method.
3. Can you also decide whether one object is "greater" than another? If such a comparison is meaningless, then it's probably best to simply write a Comparator, as Steve suggested; but if it isn't, then you could also make the object class Comparable (see java.lang.Comparable).
All of the above will help you to do what you want with existing Java classes.
Tony Docherty wrote:What class type is notRealParts?
Assuming it's a type of List, if you are doing lots of remove operations on it you may get some performance improvements by using a LinkedList.
Hello Tony - Yes notRealParts is of type List ! I will try with LinkedList.