java object search

I have a list of objects . I would like to find the object whose id property  matches with my searchKey.

whats the best way to do it ? No third party library.
cofactorAsked:
Who is Participating?
 
cofactorConnect With a Mentor Author Commented:
and that method is ..

public static String getDepartmentName(String deptId) throws SystemException{
            List<MasterVO> deptList = ApplicationContextObject.getAppContextListMap().get(AppConstants.ENGLISH_LANGUAGE).get(AppConstants.DEPARTMENTS);
            MasterVO dept = null;
            String deptName = null;
            for(int index=0;index < deptList.size();index++){
                  dept = deptList.get(index);
                  if(dept.getId().equals(deptId)){
                        deptName = dept.getName();
                        break;
                  }
            }
            return deptName;
      }
0
 
objectsCommented:
jusr uuse a map
0
 
CEHJCommented:
The fastest way is to use a Map but just implementing equals correctly and using

http://download.oracle.com/javase/6/docs/api/java/util/AbstractList.html#indexOf(java.lang.Object)

will work
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
cofactorAuthor Commented:
Not clear.  I'm planning to find the name field of a VO searching the id field.

Here is what I'm looking for. Hopefully, you get now what I'm looking at.

Input:  List  of   VO     // VO {id,name}

searchKey:  "123"

Output:  
VO with id=123 present in the list ,  this VO has a field name = "abcd"    ( // vo.getName() );
0
 
CEHJCommented:
e.g.
int ixVo = list.indexOf(new VO(123)); // Found if ixVo >= 0

Open in new window

0
 
cofactorAuthor Commented:
>>>new VO(123)

This wont work.

My VO has two fields  i.e  VO {id,name}
0
 
cofactorAuthor Commented:
Please see the input and output I have posted. You will understand what I'm looking for.
0
 
CEHJCommented:
>>My VO has two fields  i.e  VO {id,name}

Give it a ctor with just id - then it will work (if equals is implemented correctly)

Otherwise you need to use a Map
0
 
cofactorAuthor Commented:
>>>Give it a ctor with just id

Ok. I can do that.

>>>(if equals is implemented correctly)

There is no equals in my VO. Do I have to write one ? if so what would be the content of that ?


Here is the plan then :

int ixVo = list.indexOf(new VO(123));

String name=list.get(ixVo).getName();  // I get the name field of VO now
0
 
CEHJCommented:
>>Do I have to write one ? if so what would be the content of that ?

Something like
public boolean equals(Object o) {
   boolean result = this == o;
   if (!result) {
      VO other = (VO)o;
      result = (id == other.getId());
   }
   return result;
}

Open in new window

0
 
cofactorAuthor Commented:
>>>boolean result = this == o;

>>>result = (id == other.getId());

Not comfortable with this kind of notation. Could you please simplify this two line?  Alternatively could you please modify this in understandable format ?

These two lines are stopper.
0
 
cofactorAuthor Commented:
>>>jusr uuse a map

I have a list .  Unless you construct a map by iterating the list,  this wont make much sense . so, I wish if I could use the list :)
0
 
cofactorAuthor Commented:
@CHEJ, your code is not working and also there is no error in the exception block.

Here is the actual code and also the server console print
try
{
.........
LOGGER.debug("deptid # N1="+deptid);
int matchIdx=getDepartmentList().indexOf(new MasterVO(deptid));
LOGGER.debug("matchIdx N1="+matchIdx);
..............
} catch (Exception e) {
LOGGER.debug("error="+e);
}

Server Console log print:
----------------
deptid # N1=1
0
 
CEHJCommented:
>>
Server Console log print:
----------------
deptid # N1=1
>>

How is that 'not working'?
0
 
cofactorAuthor Commented:
>>>How is that 'not working'?

LOGGER.debug("matchIdx N1="+matchIdx);

This print vanished.
0
 
cofactorAuthor Commented:
I have found another workaround.  kind of DB query though but that worked fine. I would like to get relieve from this complicated approach.  

Would like to delete this post.
0
 
CEHJCommented:
Did you get an error instead?
0
 
cofactorAuthor Commented:
>>>Did you get an error instead?

No . Thats surprising .
0
 
cofactorAuthor Commented:
>>No .

In fact this is the last two line of the server

deptid # N1=1
ERROR LoginInterceptor:53 -    

user is logged in . user is a valid  in user. There is no point why Struts 2  interceptor throwing error.

I have modified the code this way now and this is working fine.

String deptid=cvo.getDeptId();
LOGGER.debug("deptid # N1="+deptid);
String deptName=CommonBusiness.getDepartmentName(deptid);
0
 
CEHJCommented:
>>
deptid # N1=1
ERROR LoginInterceptor:53 -    

user is logged in . user is a valid  in user. There is no point why Struts 2  interceptor throwing error.
>>

A possible reasons

>>int matchIdx=getDepartmentList().indexOf(new MasterVO(deptid));

getDepartmentList is returning null?

>>and that method is ..

That's what Java will do for you without having to write that code, but if you want to then - ok.

Are you getting the List in a different way in that code?
0
 
CEHJCommented:
btw, the correct equals method (which you should probably have in your class anyway) would be
public boolean equals(Object o) {
	boolean result = (this == o);
	if (!result) {
		MasterVO other = (MasterVO)o;
		result = (id.equals(other.getId()));
	}
        return result;
}

Open in new window

0
 
cofactorAuthor Commented:
>>>getDepartmentList is returning null?

No...its not null.

>>Are you getting the List in a different way in that code?
Yes. its a Struts 2 , Action field. This list is a populated list which I display in JSP.
0
 
cofactorAuthor Commented:
I would like to close this post as I have followed another approach to solve it.
0
 
cofactorAuthor Commented:
This  approach worked fine.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.