1

I hold the following map

      Map<Integer, List<String>>

when I add a new string to a certain int I just add it to the list of that int value in my map.
Now, I also need to remove it from the last list it was located in.
I will elaborate for a second :
It is not the same instance of the string. I get an int and a String, I need to add it and remove it from the former List in the map that held the same string value.

This means that I need to hold another map of String to Integer ?
Do I need to maintain two collections?
thanks.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Bick
  • 17,833
  • 52
  • 146
  • 251
  • See [link](http://stackoverflow.com/questions/3430170/how-to-create-a-2-way-map-in-java) – Omnaest May 08 '11 at 07:36
  • @User: Although the titles are similar, that link describes a 1:1 relationship between the `int` and the `String`. If I'm reading the above correctly, it's not 1:1, an `int` refers to a `List` of `String`. – T.J. Crowder May 08 '11 at 07:48
  • @user: See also: http://stackoverflow.com/questions/5828647/an-interchangeable-key-value-hashmap-set-structure/5828707#5828707 – Dave Jarvis May 08 '11 at 08:07

2 Answers2

4

It's kind of like a BiMap, but since your relationship is assymetrical (one int refers to a List of Strings whereas a String can only refer to one int), it's not going to be a perfect mtach.

It sounds to me as though you want two maps, the first being your Map<Integer, List<String>> and the second being a Map<String, Integer>. You'd want to encapsulate this in class, of course. Adding a new string is then:

  1. If there's an entry for the String in Map<String, Integer>,
    • Get the Integer from that list.
    • Use it to look up the List from your Map<Integer, List<String>> and remove the String from it.
  2. Add the String to the relevant List in your Map<Integer, List<String>>
  3. Add an entry mapping the String to the Integer in the Map<String, Integer>.
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Just a short question to the OP. Are you sure that all your strings are unique? Can two different ints map to the same string? This won't work in that case. – Kaj May 08 '11 at 08:03
  • @Kaj: In the question he says *"when I add a new string to a certain int I just add it to the list of that int value in my map. Now, I also need to remove it from the last list it was located in."* which I think is pretty clear. – T.J. Crowder May 08 '11 at 08:05
  • 1
    It's very clear for the direction of an int to a string, but that does not say anything about if the strings are unique over the whole map. I don't know anything about the data of the OP. But this will fail if 1 maps to "foo" and 42 also maps to "foo". It will work for int -> string, but not the other way around unless the strings are unique over the whole map. – Kaj May 08 '11 at 08:16
  • @Kaj: He specifically says that when adding a string "to a certain int", he "...also need[s] to remove it from the last list it was located in." That tells us that a `String` can be related to at most one `int`. Well, that plus the fact he accepted the answer. ;-) – T.J. Crowder May 08 '11 at 08:22
  • I know that he accepted the answer, and I know that it's most likely that your answer is 100% correct. Just wanted to make sure that he wouldn't get into trouble in the future because he hadn't thought about if all strings are unique or not. He says "add a new string to a certain int". That does to me only mean that the string is new for that int. It doesn't say that it hasn't been mapped to another int already. – Kaj May 08 '11 at 08:34
  • @Kaj: (Don't worry, I understand you're just trying to help the guy out.) *"It doesn't say that it hasn't been mapped to another int already."* The key part is the second half of that sentence: "I also need to remove it from the last list it was located in". With that rule in place, we know for certain that a string *cannot* ever be in the lists of two ints at the same time. – T.J. Crowder May 08 '11 at 08:47
  • I've seen that part, but I've seen lots and lots of posts (I'm not new to forums, I have about 30k posts on the javaforums) where people haven't been thinking it through. The part that I quoted indicates to me that he's thinking in the context of one int and a list, and not in the context of the whole map. I'm however guessing and agree that I'm most likely wrong :) – Kaj May 08 '11 at 08:53
  • @Kaj: Yeah, people do sometimes make mistakes in their wording, no question. :-) – T.J. Crowder May 08 '11 at 08:57
  • Hey guyz :-0) . My strings are unique (originally from toString() of a sequence). (And this debate reminded me again that this site is amazing ) 10x. – Bick May 08 '11 at 11:58
2

You could try using Guava's BiMap.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130