Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java object search

Posted on 2011-04-21
25
Medium Priority
?
404 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.
0
Comment
Question by:cofactor
  • 15
  • 8
  • 2
25 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35439805
jusr uuse a map
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35439814
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
 
LVL 92

Expert Comment

by:objects
ID: 35439821
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:cofactor
ID: 35439922
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
 
LVL 86

Expert Comment

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

Open in new window

0
 

Author Comment

by:cofactor
ID: 35439951
>>>new VO(123)

This wont work.

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

Author Comment

by:cofactor
ID: 35439964
Please see the input and output I have posted. You will understand what I'm looking for.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35439981
>>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
 

Author Comment

by:cofactor
ID: 35440026
>>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35440088
>>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
 

Author Comment

by:cofactor
ID: 35440151
>>>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
 

Author Comment

by:cofactor
ID: 35440172
>>>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
 

Author Comment

by:cofactor
ID: 35440314
@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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35440399
>>
Server Console log print:
----------------
deptid # N1=1
>>

How is that 'not working'?
0
 

Author Comment

by:cofactor
ID: 35440411
>>>How is that 'not working'?

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

This print vanished.
0
 

Author Comment

by:cofactor
ID: 35440436
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35440440
Did you get an error instead?
0
 

Author Comment

by:cofactor
ID: 35440484
>>>Did you get an error instead?

No . Thats surprising .
0
 

Author Comment

by:cofactor
ID: 35440533
>>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
 

Accepted Solution

by:
cofactor earned 0 total points
ID: 35440554
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35440646
>>
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35440926
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
 

Author Comment

by:cofactor
ID: 35458484
>>>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
 

Author Comment

by:cofactor
ID: 35458515
I would like to close this post as I have followed another approach to solve it.
0
 

Author Closing Comment

by:cofactor
ID: 35496392
This  approach worked fine.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question