java + increasing performance and scalability
- by varun
Hi,
below is the a code snippet, which returns the object of a class. now the object is basially comparing to some parameter in loop.
my concern is what if there are thousands of objects in loop, in that case performance and scalability can be an issue. please suggest how to improve this code for performance part
public Widget get(String name,int major,int minor,boolean exact) {
Widget widgetToReturn = null;
if(exact) {
Widget w = new Widget(name, major, minor);
// for loop using JDK 1.5 version
for(Widget wid : set) {
if((w.getName().equals(wid.getName())) && (wid.getVersion()).equals(w.getVersion())) {
widgetToReturn = w;
break;
}
}
} else {
Widget w = new Widget(name, major, minor);
WidgetVersion widgetVersion = new WidgetVersion(major, minor);
// for loop using JDK 1.5 version
for(Widget wid : set) {
WidgetVersion wv = wid.getVersion();
if((w.getName().equals(wid.getName())) && major == wv.getMajor() && WidgetVersion.isCompatibleAndNewer(wv, widgetVersion)) {
widgetToReturn = wid;
} else if((w.getName().equals(wid.getName())) && wv.equals(widgetVersion.getMajor(), widgetVersion.getMinor())) {
widgetToReturn = w;
}
}
}
return widgetToReturn;
}