4 Replies Latest reply: Nov 21, 2010 4:03 AM by YoungWinston RSS

    Collection to hold object type? OR typical hash in java?

    760959
      Hi folks,
      I am used to work perl and use hashes a lot (a typical perl hash is string => string). Well now I am trying to do something a little dirty - I need an object that does what the class that I implemented down there does...Is there such an object that wouldnt mind that both keys and values arent unique (there can be same values among both keys and values). Do I have to implement it myself or is there something that does that...
      No collection seems to match this...


      public class CantFindYouInJava {
      private String[] listOfKeys;
      private String[] listOfValues;
      public void put(String key,String value){
      int where=listOfkeys.length+1;
      listOfKeys[where]=key;
      listOfValues[where]=value;
      }
      ..
      }
        • 1. Re: Collection to hold object type? OR typical hash in java?
          Kayaman
          Err, so are you saying that you want a Map that doesn't mind duplicate keys?

          There's no standard Java Collection for it, but Apache Commons Collections has a MultiMap (or whatever it's called) that allows you to map multiple values to the same key.
          • 2. Re: Collection to hold object type? OR typical hash in java?
            YoungWinston
            koszta5 wrote:
            I need an object that does what the class that I implemented down there does...Is there such an object that wouldnt mind that both keys and values arent unique (there can be same values among both keys and values). Do I have to implement it myself or is there something that does that...
            It really depends on what the relationship is between a key and its related value, and the sort of performance you expect for retrieval of either/both.

            Kayaman's suggestion is probably the best if the Key-Value relationship is 1-to-many; although you could also write your own Map<String, Iterable<String>> class. If it truly is a many-to-many, and you need good performance in both directions, you might need to wrap two MultiMaps - one to go from key to values, and a second to go the other way. Not sure what you'd call it though.

            Winston
            • 3. Re: Collection to hold object type? OR typical hash in java?
              760959
              Yes, that is exactly what I need... I truly need many-to-many hash-like object. I will just stick with writing my own class. The thing is (luckily) I am only going to get values going from the beginning to the end so there wont anything like get me all the values that correspond with the key "x" ... I will just have to implement an iterator for my class...
              thanks anyway guys
              • 4. Re: Collection to hold object type? OR typical hash in java?
                YoungWinston
                koszta5 wrote:
                Yes, that is exactly what I need... I truly need many-to-many hash-like object. I will just stick with writing my own class. The thing is (luckily) I am only going to get values going from the beginning to the end so there wont anything like get me all the values that correspond with the key "x" ...
                Can you provide examples of the type of access you'll need, or maybe tell us what your "key" and "value" strings are?
                From what you describe, I'm wondering if you simply have two independent lists.

                A many-to-many relationship is typically (at least in databases) implemented using a cross-reference matrix and two one-to-many relationships, viz:
                Item1 --->> Cross-ref <<--- Item2
                where the cross-reference holds all valid combinations of Item1+Item2; but in a true many-to-many there is no idea of a "key" and a "value".

                Winston