aspose file tools*
The moose likes Java in General and the fly likes Check one of the property of objects contains the given value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Check one of the property of objects contains the given value" Watch "Check one of the property of objects contains the given value" New topic
Author

Check one of the property of objects contains the given value

Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi,

I've List of student Objects

class Student{
int rollNo,
string name;
}

List<Student> studentList={Student1(16,"madhavi"),Student2(56,"devi"),Student3(356,"subu") ....... Student200(656,"raji")}

I'm iterating the object from i=1 to 100. While iterating, i want to check that the list contains the student who having the roll number in value of i or not.

if any match i found, i have to get that student information.

Do we have any build in method to do this process fast.

I've tried to use Lambda select features. but i got syntax error.

and I've tried to use hashmap to do this process. It is working. But it gives poor performance and took memory space lot for 1000 objects.

Can any one suggest good coding for this process
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You already have it on an ArrayList - so why not just use a for-each loop and parse each objects and compare its roll number with a value - you end up with created objects + one ArrayList object...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Madhavi Subramaniam wrote:I've tried to use hashmap to do this process. It is working. But it gives poor performance

What were you using as a key ? A HashMap with roll number as key and Student objects as values should give you very good performance.

Joanne
Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi John Jai,


Using Array of objects is better than hashmap?

But i like to use array of objects. If i use array of objects,whether it will affect performance?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I mentioned ArrayList. But if you ask Arrays - yes I have read its faster but not good for your requirement as you deal with a collection of objects and there is Collection API for that.

You can go the way Joanne tells - use the HashMap key as the roll no. So it might look like HashMap<Integer,Student> and iterate over the key set of Integers and compare it with the give value.

By that way you also ensure no duplicate students exist in the HashMap as it prevents from having duplicate Integer keys.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
John Jai wrote:and iterate over the key set of Integers and compare it with the give value.

No need to iterate even... you can use containsKey() and get() method to check and retrieve if the given value is a roll number of the student.
Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33

Ok.Thanks John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check one of the property of objects contains the given value