We help IT Professionals succeed at work.

java object search

cofactor
cofactor asked
on
Medium Priority
428 Views
Last Modified: 2012-05-11
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.
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
jusr uuse a map
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

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() );
CERTIFIED EXPERT
Top Expert 2016

Commented:
e.g.
int ixVo = list.indexOf(new VO(123)); // Found if ixVo >= 0

Open in new window

Author

Commented:
>>>new VO(123)

This wont work.

My VO has two fields  i.e  VO {id,name}

Author

Commented:
Please see the input and output I have posted. You will understand what I'm looking for.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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

Author

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
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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

Author

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.

Author

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 :)

Author

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
CERTIFIED EXPERT
Top Expert 2016

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

How is that 'not working'?

Author

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

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

This print vanished.

Author

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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Did you get an error instead?

Author

Commented:
>>>Did you get an error instead?

No . Thats surprising .

Author

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);
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

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.

Author

Commented:
I would like to close this post as I have followed another approach to solve it.

Author

Commented:
This  approach worked fine.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.