10 Replies Latest reply: Nov 13, 2007 9:43 PM by 807603 RSS

    HashTable value has two

    807603
      Hello,
      I am going to create a HashTable, each key has two corresponding values.
      One is integer, the other is a string.
      How to declare it?


      Thanks
        • 1. Re: HashTable value has two
          807603
          Are both data significant in determining the uniqueness of the key?

          For example, are these two keys considered different?
          { 15, "dog" }
          { 15, "cat" }

          How about these?
          { 15, "dog" }
          { 27, "dog" }
          • 2. Re: HashTable value has two
            807603
            No, I mean.
            a==>"dog","15";
            b==>"cat",18";


            a, b are keys.

            How to join "dog", "15" together with one?
            I don't want to define an array x=["dog","15], then a==>x.
            I know use Object, but how to?

            Thanks
            • 3. Re: HashTable value has two
              807603
              Oh. Just define a class that holds those two values.

              Hell, I'll do it for you, it's so trivial:
              public class Pair {
                public final int theInteger;
                public final String theString;
                public Pair(int i, String s) {
                  theInteger = i;
                  theString = s;
                }
              }
              Because they're final, it's relatively safe to leave those values exposed. But you can create accessor methods for them (getters) instead, and make the fields private. If the values are going to change, you should definitely make the fields private.
              • 4. Re: HashTable value has two
                807603
                Appreciate!
                • 5. Re: HashTable value has two
                  807603
                  One more question.
                  I define a HashMap, I want to extract the values to the buffers.
                   private HashMap<String, Object> sta;
                   sta = new HashMap<String, Object>();
                  
                  for (int i = 0;i < aLines.length; i++) {
                                   Pair pair = new Pair(i,author);
                             
                                  sta.put(aLines.trim(), pair);
                  }
                  ............
                  Set<String> keys = sta.keySet();
                  int j = 0;
                  for(String key : keys){
                  StringBuffer buffer1 = new StringBuffer();
                  buffer1.append(sta.get(key));
                  // then how to add sta's values to the buffer
                  is 
                   
                  buffer1.append(sta.values().); ??????
                  Regards!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                  • 6. Re: HashTable value has two
                    807603
                    have your class define a toString method override then call that method of the class to create a string that gets loaded into the stringbuffer.
                    • 7. Re: HashTable value has two
                      807603
                      I got a wrong output.
                      why?
                      import java.lang.*;
                      public class Pair {
                        private double theDur;
                        private String theString;
                        
                       public Pair(double i, String s) {
                          theDur = i;
                          theString = s;
                        }
                      
                        public String toString() {
                          return String.format("%.2f    %s",theDur,theString);
                        }
                      
                      }
                       for (int i = 0;i < aLines.length; i++) {
                                       Pair pair = new Pair(0,author);
                                 
                                      sta.put(aLines.trim(), pair.toString());
                      }
                      ......
                      Set<String> keys = sta.keySet();
                      ......
                      for(String key : keys){
                      StringBuffer buffer1 = new StringBuffer();
                      buffer1.append(sta.get(key));
                      buffer1.append(" ");
                      buffer1.append(sta.values().toString());
                      j++;
                      buffer1.append("\n");
                      out1.write(buffer1.toString());
                      out1.flush();
                      System.out.println(buffer1); // wrong here
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                      • 8. Re: HashTable value has two
                        807603
                        How was it wrong?

                        Did you get something else instead of what you expected? If so, what?

                        [add]

                        I can't help noticing that for every key, you're adding the value to the buffer, and then all values. Why are you doing that?

                        Edited by: paulcw on Nov 13, 2007 9:09 AM
                        • 9. Re: HashTable value has two
                          807603
                          Oh, I found
                          buffer1.append(sta.values().toString());
                          add all values to the buffer.
                          So how to add each individual value?
                          • 10. Re: HashTable value has two
                            807603
                            ignore it.