4 Replies Latest reply: Apr 5, 2012 11:12 AM by 928743 RSS

    Creating collections from a String

    928743
      I have a scenario for which I am looking for the most efficient approach.

      I have key value pairs coming to the system in form of a long string
      for e.g. K1,V1;K1,V2;K2,V3;K2V4;K3,V5
      I need to display it in form of

      K1: V1,V2
      K2: V3,V4
      K3: V5

      Approach I am thinking:
      Steps:
      1. create string tokens token 1 - K1,V1 token 2 - K1V2 etc, add them to a list KeyValuesRawList
      2. create string tokens token 1 K1, token 2 K2 etc, add them to a list KeyList
      4. create a map finalMap, lists valueList1,valueList2,valueList3
      3. Iterate through KeyValuesRawList,
      3.1 create tokens tempK and tempV
      3.2 if (tempK = K1)
      add tempV to valueList1
      if(tempK = K2)
      add tempV to valueList2
      if(tempK = K3)
      add tempV to valueList3
      3.3 Add values from list KeyList as keys to finalMap
      3.4 Add values from lists valueList1,valueList2,valueList3 as values to finalMap
        • 1. Re: Creating collections from a String
          gimbal2
          Basically your situation is that you have a "stream" of tokens in which the same key may appear multiple times but with a different value; in other words you have one key which is bound to multiple values.

          I don't know, but a HashMap with an ArrayList as its value can easily store that. It would be something like:
          Map<String,List<String>> values = new HashMap<String,List<String>>();
          1. tokenize
          2. for each key/value pair...
          2a. fetch List from HashMap for key K
          2b. if list is null, create it (ArrayList) and put it in the HashMap under key K
          2c. add value V to the list

          Nice and dumb.
          • 2. Re: Creating collections from a String
            796440
            Crosspost: http://www.coderanch.com/t/572621/java/java/Creating-collections-String
            • 3. Re: Creating collections from a String
              tschodt
              I would suggest parsing your input should give you those "tokens" you are talking about,
              you should store these keys and values to a Map<String,List<String>>
              or possibly a Map<String,Set<String>>, depending on how you want to handle duplicate value entries.
              java KeyValueList "K1,V1;K1,V2;K2,V3;K2,V4;K3,V5;K3,V5"
              K1: [V1, V2]
              K2: [V3, V4]
              K3: [V5, V5]
              
              java KeyValueSet "K1,V1;K1,V2;K2,V3;K2,V4;K3,V5;K3,V5"
              K1: [V2, V1]
              K2: [V3, V4]
              K3: [V5]
              • 4. Re: Creating collections from a String
                928743
                Thanks everyone for your time and suggestions.
                As suggested by you I am using HashMap with String as key and ArrayList<String> as value.
                And to add values to the map I am using multimap like implementation, you can refer to the crosspost also as mentioned in the above posts.