9 Replies Latest reply: Jun 18, 2012 6:03 AM by sabre150 RSS

    Check for duplicate key in hashmap

    715138
      Hi All,

      I have a String array which has underscore separated values.

      e.g :String str[]=new String[]{"batchId+bemId_only_bentype_ben_id","batcytrd_only_bentype_ben_id"};

      now we have to traverse this array and and store the value before underscore as a key for hashmap and rest as value for hashmap.

      Here "batchId+bemId" will be as a key and "only_bentype_ben_id" will be value .similarly for second array element the key will be "batcytrd" and value will be "only_bentype_ben_id".

      How to achive this .I am not able to split the element as key and value .

      My sample code is like this :This is not complete code :

      import java.util.StringTokenizer;



      public class DuplicateCheck {

           public static void main(String[] args) {
      String str[]=new String[]{"batchId+bemId_only_bentype_ben_id","batchId+bemId_only_bentype_ben_id"};
      String str1;
      StringTokenizer st2;
      for (int i=0;i<str.length;i++){
           str1=str;
           st2 = new StringTokenizer(str1, "_");
           
           while(st2.hasMoreTokens()) {
                String key = st2.nextToken();
                System.out.println(key );
           }
      }
           }
      }


      Thanks
      Sumit
        • 1. Re: Check for duplicate key in hashmap
          sabre150
          Use String.indexOf() to find the index of the '_' and then use String.substring() to extract the two parts. I don't see anything in your description or code dealing with 'duplicate key' issues so the title of your thread seems to be misleading.
          • 2. Re: Check for duplicate key in hashmap after getting them from string array
            715138
            Hi,

            Thanks for your reply.Actully after getting the key/value from array I need to put them in a hashmap .If the key already exists then get that key and return to the calling function.However I am trying to do it as you suggested.

            Thanks
            Sumit
            • 3. Re: Check for duplicate key in hashmap after getting them from string array
              DrClap
              You have two steps in your requirement:

              1. Extract the key and the value from the input data.

              2. Put the key and data into a hashmap and report something if the key already exists.

              All of the discussion so far, including all of your contributions, address only the first step. If you were assuming that the second step would happen automatically based on what you have seen so far, that is not correct. You have to write that code (which is pretty trivial and requires maybe four lines).
              • 4. Re: Check for duplicate key in hashmap after getting them from string array
                EJP
                If the key already exists, you don't need to 'get that key': you already have it. Do you perhaps mean 'get the value for that key'? I suggest part of the problem here is the vagueness of your requirements, or of your understanding.
                • 5. Re: Check for duplicate key in hashmap
                  TPD-Opitz
                  user8687839 wrote:> String str[]=new String[]{"batchId+bemId_only_bentype_ben_id","batcytrd_only_bentype_ben_id"};

                  Here "batchId+bemId" will be as a key and "only_bentype_ben_id" will be value .similarly for second array element the key will be "batcytrd" and value will be "only_bentype_ben_id".

                  My sample code is like this :
                  import java.util.StringTokenizer;
                  public class DuplicateCheck {
                       public static void main(String[] args) {
                  String str[]=new String[]{"batchId+bemId_only_bentype_ben_id","batchId+bemId_only_bentype_ben_id"};
                  String str1;
                  StringTokenizer st2;
                  for (int i=0;i<str.length;i++){
                  str1=str;
                  st2 = new StringTokenizer(str1, "_");
                  while(st2.hasMoreTokens()) {
                  String key = st2.nextToken();
                  System.out.println(key );
                  }
                  }
                  }
                  }
                  You'd better use <tt>String.split()</tt>
                  http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int)
                  for (String input : str){
                     String inputParts = input.split("_",2);
                     System.out.println("key  = " +inputParts[0]);
                     System.out.println("value= " +inputParts[1]); // throws ArrayIndexOutOfBounds if no underscore in 'input'
                  }
                  bye
                  TPD
                  • 6. Re: Check for duplicate key in hashmap
                    sabre150
                    T.PD wrote:
                    You'd better use <tt>String.split()</tt>
                    I have to disagree here. As regulars to these forums will know I am a great fan of regular expressions but as I pointed out in the first response to this thread in this case a simple use of String.indexof() will do.
                    • 7. Re: Check for duplicate key in hashmap
                      TPD-Opitz
                      sabre150 wrote:
                      T.PD wrote:
                      You'd better use <tt>String.split()</tt>
                      I have to disagree here. As regulars to these forums will know I am a great fan of regular expressions but as I pointed out in the first response to this thread in this case a simple use of String.indexof() will do.
                      Here I_ have to disagree...
                      After <tt>String.indexof()</tt> you have the position of the first underscore. You need more code to split the actual string it the pieces needed.

                      So yes: <tt>String.indexof()</tt> can be part of the solution, but it will not deliver the desitered substrings (of its own) while <tt>String.split()</tt> will do (with the limit parameter).

                      bye
                      TPD
                      • 8. Re: Check for duplicate key in hashmap
                        sabre150
                        T.PD wrote:
                        have to disagree here.
                        Lets agree to disagree then :-)
                        • 9. Re: Check for duplicate key in hashmap
                          TPD-Opitz
                          sabre150 wrote:
                          Lets agree to disagree then :-)
                          agreed ;o)