Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

jusr uuse a map
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
Avatar of cofactor
cofactor

ASKER

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() );
e.g.
int ixVo = list.indexOf(new VO(123)); // Found if ixVo >= 0

Open in new window

>>>new VO(123)

This wont work.

My VO has two fields  i.e  VO {id,name}
Please see the input and output I have posted. You will understand what I'm looking for.
>>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
>>>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
>>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

>>>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.
>>>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 :)
@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
>>
Server Console log print:
----------------
deptid # N1=1
>>

How is that 'not working'?
>>>How is that 'not working'?

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

This print vanished.
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.
Did you get an error instead?
>>>Did you get an error instead?

No . Thats surprising .
>>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);
ASKER CERTIFIED SOLUTION
Avatar of cofactor
cofactor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>
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?
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

>>>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.
I would like to close this post as I have followed another approach to solve it.
This  approach worked fine.