Results 1 to 15 of 15
  1. #1
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Mapping a set to a string

    Hello,
    I wrote a code that takes a text file and split it in 2 parts. I'm storing the first part as the key while the other part is stored as the value. It all worked fine until I had to deal with a text file that resulted in multiple same keys with different values. Now when I try to get the value of one of the duplicated keys, I don't get the correct value.

    my map looks something like this:
    map.add( "1" , "2")
    map.add("1" , "3")
    map.add("1", "4")

    so when I want to retrieve the value of the first key, I don't get 2, I get 4.
    I'm looking for a way in which I can eliminate duplicated keys and map the one remaining to a set of strings that holds all the possible values for that key. Is there any clue of how I can do that after inputting the text file and splitting its content?

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: Mapping a set to a string

    The first thought that comes to mind is storing a LinkedList instead of a String. It's a bit more complicated though; instead of just adding, you'd have to get the LinkedList from the map, and if none existed, create a new one and add it to the map. Then add the new String to the LinkedList. There's no reason to add the LinkedList back to the map, since it's already stored there, although it doesn't hurt anything if you do anyway.

  3. #3
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping a set to a string

    Thank you for your reply! Can you explain it a little more.
    Here is the code I'm using to add the keys and values to the map:

    while ((strLine = br.readLine()) != null) {
    //System.out.println(strLine);

    String [] splitOut = strLine.split(":");

    //Adding the ID & value to the map
    value.add(splitOut[1]);
    key.add(splitOut[0]);

    ID_MAP.put( splitOut[1] , splitOut[0]);

    }

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: Mapping a set to a string

    What is the class of ID_MAP? Is it something you've done yourself, or is it one of the standard map classes?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Mapping a set to a string

    Any given key can only map to one value. The way to solve your problem is to have a map of arrays of type String. So when you have a specific key, you either grab the array at the location and add the string, create the array, and add the string and then store it in the map.

    Here would be an example declaration:
    Java Code:
    Map<String,List<String>> map = new HashMap<>();
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping a set to a string

    Thanks Jim for your answer! This is what I'm trying to do. But I'm having difficulty creating the list that will map to that one key. should I first add the keys and values to a map and then loop through the map to check for duplicated keys and then store the values is a list or set? Can you show me with code how to do this?

    @Toll: thanks for your attention, this is what i'm using to define the map: [/QUOTE]Map<String, String> ID_MAP = new HashMap<>();[/QUOTE]

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: Mapping a set to a string

    Ah, excellent. Instead of making a HashMap<String, String>, you can make a HashMap<String, LinkedList<String>>. As I said though, the adding is a bit more complex.

    First, you get the value stored for the key you just read. If this value is null, you create a new LinkedList<String> and put it in the map as the value for the key you just read.

    After that, you add the newly-read value to the LinkedList stored in the Map.

    It is a bit more complicated, as I said, so if I'm unclear anywhere, I should be able to clarify.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Mapping a set to a string

    You don't have to add the Lists (probably an ArrayList) ahead of time. As Toll had already suggested, just use the key to get the List. If the list is null, create one and add it to the map and add the string to the list. If it is already there, just grab the list reference and add the string to the list.

    Regards,
    Jim
    Last edited by jim829; 07-12-2013 at 08:38 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping a set to a string

    Toll and Jim,
    Thank for your ideas. But I couldn't understand exactly what you were pointing to. Do u mean that I only need to creat one map and compare the keys that will be added to the map befor adding them or after? What do u mean by saying "if the list is null"?

  10. #10
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: Mapping a set to a string

    You only need one map, yes.

    When it comes to adding values, you don't add them directly, but rather get the list for that key and add the value to that list. What's important to know here is that if no list has been created for that key yet, then it will return null. If so you need to create the list and add it to the map.

  11. #11
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping a set to a string

    Ok! My issue is in the step of creating the list for each key. I have a really big file of Strings, and what i'm doing is splitting it into keys and values. Both keys and values are stored in arrays. So my issue is how to eliminate duplicated values in the Keys array and at the same time store their corresponding values in a list?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Mapping a set to a string

    You can only have one key per value in a map. And since your data (according to you) has a one to many mapping of keys to data you need to store the data in another List (which is the data for the key). The duplicate keys will go away by the nature of the way maps behave. Here is a visual reference of what I believe you want to do:

    "A", "Apple"
    "B", "Bear"
    "A","Anteater"
    "B", "Boar"
    "A", "Aphid"
    "C", "Camera"

    After creating the map:
    "A" -> ["Apple", "Anteater", "Aphid"]
    "B" -> ["Bear", "Boar"]
    "C" -> ["Camera"]

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: Mapping a set to a string

    If you associate the tulple <K,V> in your map, the key K will be associated with a List that contains V; but what do you want to happen when you associate the tuple <K,V> twice (or more) in the map? Do you want the value List to contain two (or more) values V? If not, better use a Map<K, Set<V>> instead of a Map<K, List<V>>.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping a set to a string

    What I want to do is first eliminate duplicated key, and have the values of each unique key stored in a Set of Strings.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Mapping a set to a string

    Good point. But we hadn't gotten that far in the discussion yet (at least not as far as I could discern).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2013, 10:54 AM
  2. Mapping Key Events
    By Dark in forum New To Java
    Replies: 34
    Last Post: 06-26-2012, 03:04 AM
  3. help with mapping and relationship
    By peace76 in forum Enterprise JavaBeans (EJB)
    Replies: 5
    Last Post: 12-07-2010, 07:12 PM
  4. Help with image mapping
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:06 AM
  5. About servlet-mapping
    By Heather in forum Java Servlet
    Replies: 1
    Last Post: 07-14-2007, 06:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •