This discussion is archived
4 Replies Latest reply: Mar 13, 2011 1:49 PM by jschellSomeoneStoleMyAlias RSS

Problem with map data structure in java

845642 Newbie
Currently Being Moderated
Hi, i intend to store names with phone numbers under the following constraint:
1.If the same person has two different phone numbers then the latest one gets stored.
2.If two different persons have the same phone number then both the entries should not appear in the list
3.If there are two similar entries, then only one of them should appear.

I initially thought of using Map in java, but for situations like:

Rahul 56789
Ramesh 12345
Ramesh 56789

all entries will be removed ,however Ramesh 12345 should be there. So how to go about it?
Thanks.
  • 1. Re: Problem with map data structure in java
    tschodt Pro
    Currently Being Moderated
    pranay wrote:
    i intend to store names with phone numbers under the following constraint:
    1.If the same person has two different phone numbers then the latest one gets stored.
    2.If two different persons have the same phone number then both the entries should not appear in the list
    3.If there are two similar entries, then only one of them should appear.

    I initially thought of using Map in java, but for situations like:

    Rahul 56789
    Ramesh 12345
    Ramesh 56789

    all entries will be removed ,however Ramesh 12345 should be there. So how to go about it?
    Reorder the rules.
    1 (was 2). If two different persons have the same phone number then both the entries should not appear in the list
    2 (was 1). If the same person has two different phone numbers then the latest one gets stored.
    3. If there are two similar entries, then only one of them should appear.

    What does
    both the entries should not appear in the list
    mean?
    neither entry should appear?
    or
    only the first?
    or
    only the last?
  • 2. Re: Problem with map data structure in java
    YoungWinston Expert
    Currently Being Moderated
    pranay wrote:
    I initially thought of using Map in java, but for situations like:
    Rahul 56789
    Ramesh 12345
    Ramesh 56789
    all entries will be removed
    No they won't; indeed if the Map key is a combo of both name and phone number none of them will.

    It seems to me that you actually want two Maps: one keyed by Name and one keyed by phone number; however the rules as you put them don't state exactly what you want to happen in the case of two people sharing the same phone.

    For example, the ordering you have above suggests that Ramesh used to have the phone number 12345, but has now moved in with Rahul and so has changed his phone number. Which one should be displayed for phone number 56789?

    Winston
  • 3. Re: Problem with map data structure in java
    845642 Newbie
    Currently Being Moderated
    >

    >
    For example, the ordering you have above suggests that Ramesh used to have the phone number 12345, but has now moved in with Rahul and so has changed his phone number. Which one should be displayed for phone number 56789?

    Winston
    In that case there would be no entry for the number 56789, however, i did arrive at a solution by using condition statements and its working fine :)
    Thanks everyone for helping . :)
  • 4. Re: Problem with map data structure in java
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    pranay wrote:
    3.If there are two similar entries, then only one of them should appear.
    That requirement as stated can't really be implement by a human much less a computer.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points