Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-06-2008, 08:46 PM
Member
 
Join Date: Mar 2008
Posts: 2
desmond5 is on a distinguished road
Retreiving list of keys from Map with certan value
Hello,

I have a map (doesn't matter if it's hashmap or treemap or whaterer, just as long as it has keys and values).
I have several keys (of Person) in the map that have the same value (of Job).

Quote:
public class Person
{
private String name;

public Person (String name) {
this.name = name;
}

@Override
public String toString() {
return this.name;
}
}

public class Job {

private String name;

public Job (String name) {
this.name = name;
}

@Override
public String toString() {
return this.name;
}
}
How can I retreive a list of keys which have the same value in the map ? Or In another words, how can I retreive a list of persons who all have the same Job ?
Is a map a good choice here at all ?

Many many thanks and best wishes,
desmond5
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-06-2008, 09:10 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Make up a method that will take the Job you are looking for and collect all Person keys in the map that have the same value as the Job argument.
Code:
Map<Person, Job> map = new HashMap<Person, Job>(); private List<Person> getPeopleByJob(Job job) { List<Person> list = new ArrayList<Person>(); Set<Person> keys = map.keySet(); Iterator<Person> it = keys.iterator(); while(it.hasNext()) Person person = (Person)it.next(); if(map.get(person).equals(job)) list.add(person); } return list; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retreiving of mail body using mail number chandu.v09 JavaServer Pages (JSP) and JSTL 0 03-13-2008 03:25 PM
Plz help ... retreiving data from an access database table.... austinsmiles New To Java 1 02-01-2008 02:21 PM
Uisng primitive type values as keys for Hashtable ravian New To Java 3 11-21-2007 11:13 AM
list Zensai New To Java 3 11-20-2007 08:22 AM
How to get the max value from a list osval New To Java 1 07-30-2007 06:43 PM


All times are GMT +3. The time now is 08:43 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org