Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check one of the property of objects contains the given value

 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok.Thanks John.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic