How to delete an entry from a Map?

W

www

Hi,

I cannot figure out how to delete an entry from a Map(HashMap). For
example, suppose I have a class Person and 3 objects of that
type(personA, personB and personC):


public class Person {
private String name;

....//

public String getName{
return name;
}
}

personA is with name of "John", personB and personC are with other names.

Map<Person> map = new HashMap<Person>();

//so easy to add to Map
map.add(personA);
map.add(personB);
map.add(personC);

....

//now, I want the map only has the guy with name of "John", i.e. I want
to delete personB and personC from map. I felt that it is so difficult.

Thank you for your help.
 
E

Eric Sosman

www wrote On 11/16/07 15:36,:
Hi,

I cannot figure out how to delete an entry from a Map(HashMap). For
example, suppose I have a class Person and 3 objects of that
type(personA, personB and personC):


public class Person {
private String name;

....//

public String getName{
return name;
}
}

personA is with name of "John", personB and personC are with other names.

Map<Person> map = new HashMap<Person>();

//so easy to add to Map
map.add(personA);
map.add(personB);
map.add(personC);

...

//now, I want the map only has the guy with name of "John", i.e. I want
to delete personB and personC from map. I felt that it is so difficult.

You say you're using a Map, but your code sample seems
to be using a Set (and calling it a Map).

If it's a Map, you would have something like

Map<String,Person> map = new HashMap<String,Person>();
// add Persons:
map.put(personA.getName(), personA);
map.put(personB.getName(), personB);
map.put(personC.getName(), personC);
// remove two of them:
map.remove(personB.getName());
map.remove(personC.getName());

If it's a Set, it would look like

Set<Person> set = new HashSet<Person>();
// add Persons:
set.add(personA);
set.add(personB);
set.add(personC);
// remove two of them:
set.remove(personB);
set.remove(personC);
 
R

Robert Klemme

Hi,

I cannot figure out how to delete an entry from a Map(HashMap). For
example, suppose I have a class Person and 3 objects of that
type(personA, personB and personC):


public class Person {
private String name;

....//

public String getName{
return name;
}
}

personA is with name of "John", personB and personC are with other names.

Map<Person> map = new HashMap<Person>();

//so easy to add to Map
map.add(personA);
map.add(personB);
map.add(personC);

...

//now, I want the map only has the guy with name of "John", i.e. I want
to delete personB and personC from map. I felt that it is so difficult.

Thank you for your help.

Read the Javadoc.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top