Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

How to delete an entry from a Map?

Thread view: 
www - 16 Nov 2007 20:36 GMT
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.
Eric Sosman - 16 Nov 2007 21:00 GMT
www wrote On 11/16/07 15:36,:
> Hi,
>
[quoted text clipped - 25 lines]
> //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);

Signature

Eric.Sosman@sun.com

Robert Klemme - 17 Nov 2007 23:17 GMT
> Hi,
>
[quoted text clipped - 27 lines]
>
> Thank you for your help.

Read the Javadoc.

    robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.