Collections

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    Collections

    Is there any collection in which we can have two keys for the same value?....Like in HashTable we have key k1 for value v1 I want it like key k1, k2 for value v1
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by madhoriya22
    Is there any collection in which we can have two keys for the same value?....Like in HashTable we have key k1 for value v1 I want it like key k1, k2 for value v1
    See the entry for Map.
    That means you have to make your own Map.

    Comment

    • madhoriya22
      Contributor
      • Jul 2007
      • 251

      #3
      Originally posted by r035198x
      See the entry for Map.
      That means you have to make your own Map.
      "Make your own map", do you mean by that I have to put a map into another map........Plz correct me if I am wrong?

      If you are saying the same thing which I have understood, Then I think it will decrease my code performance.... .Is there any other way to do it......

      Sorry if I misunderstood your suggestion.....

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by madhoriya22
        Sorry if I misunderstood your suggestion.....
        I don't quite understand your original question; you mentioned the associations
        <k1, v1> and <k2, v1> in your example. Does one have to supply k1 *and* k2
        in order to retrieve v1 from the wanted datastructure, or does one have to supply
        k1 *or* k2 in order to retrieve that value v1?

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          I don't quite understand your original question; you mentioned the associations
          <k1, v1> and <k2, v1> in your example. Does one have to supply k1 *and* k2
          in order to retrieve v1 from the wanted datastructure, or does one have to supply
          k1 *or* k2 in order to retrieve that value v1?

          kind regards,

          Jos
          I bet my last mint sweet that he means he wants to be able to supply k1 or k2.
          <crosses fingers>

          Comment

          • madhoriya22
            Contributor
            • Jul 2007
            • 251

            #6
            Originally posted by JosAH
            I don't quite understand your original question; you mentioned the associations
            <k1, v1> and <k2, v1> in your example. Does one have to supply k1 *and* k2
            in order to retrieve v1 from the wanted datastructure, or does one have to supply
            k1 *or* k2 in order to retrieve that value v1?

            kind regards,

            Jos
            Actually I have to recognise a value(v1), which can be only recognise by passing both the keys k1 and k2...let me explain you the whole problem

            This is the method in which I am passing WorkPackageId in the query to get no. of counts of defects based on status....for gettting the count corresponding to status I have put status as key in the HashTable...Her e is the method:-
            Code:
            public Hashtable getWorkPackageSummary(String workPackageId) {
                    
                    Connection con = null;
                    ResultSet rs = null;
                    
                    Hashtable hsht = new Hashtable();
                    String status = null;
                    try {
                        con = new MySqlDAOFactory().getConnection();
                        PreparedStatement pStatement = null;
                        pStatement = con.prepareStatement(GET_WORKPACKAGE_SUMMARY);
                        System.out.println(GET_WORKPACKAGE_SUMMARY);
                        
                        pStatement.setString(1, workPackageId);
                        System.out.println("Id:::"+workPackageId);
                        
                        rs = pStatement.executeQuery();
                        while(rs.next()) {                
                            
                            WorkPackageSummaryVO workPackageSummary = new WorkPackageSummaryVO();
                            
                            workPackageSummary.setWorkPackageName(rs.getString("WORKPACKAGE_NAME"));
                            status = rs.getString("CURRENT_DEFECT_STATUS");
                            workPackageSummary.setCount(rs.getInt("COUNT"));
                            
                            if(workPackageSummary != null) {
                                hsht.put(status, workPackageSummary);
                            }
                        }
                        rs.close();
                        pStatement.close();
                    }catch (Exception ex) {
                        System.out.println("Exception occured while retriving WorkPackage Summary from database " + ex);
                        ex.printStackTrace();
                    }finally {
                        try {
                            con.close();
                        } catch (SQLException ex) {
                            System.out.println("Exception occured while closing connection during WorkPackage Summary Retrival " + ex);
                            ex.printStackTrace();
                        }
                    }
                    return hsht;
                }
            Now this method is working fine....But doing same thing I have to get these details for all the Workpackages in the database....for that I have put a for loop...through which I am passing workpackageId one by one in the query.....now while getting the count back from HashTable.... I have to recognise both workpackage name and status to which it belongs....now the method looks like this:-
            Code:
             
            for(int i = 0; i < vct1.size(); i++) {
                            System.out.println("for i = "+i);
                            pStatement.setString(1, (String)vct1.get(i));
                            //System.out.println("Id:::"+workPackageId);
                            
                            rs = pStatement.executeQuery();
                            while(rs.next()) {
                                System.out.println("Inside while loop");
                                
                                WorkPackageSummaryVO workPackageSummary = new WorkPackageSummaryVO();
                                
                                status = rs.getString("CURRENT_DEFECT_STATUS");
                                workPackageSummary.setCurrentDefectStatus(rs.getString("WORKPACKAGE_NAME"));
                                workPackageSummary.setCount(rs.getInt("COUNT"));
                                
                                if(workPackageSummary != null) {
                                    hsht.put(status, workPackageSummary);
                                }
                            }
                        }
            I cant get this information through query...bcoz it is group by status...It is such a long description...h ope u would understand what I am trying to say here...Thanks

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              I bet my last mint sweet that he means he wants to be able to supply k1 or k2.
              <crosses fingers>
              Well, an ordinary Map can do the job then:

              [code=java]
              K k1= ...
              K k2= ...
              V v1= ...
              Map<K, V> map= ...

              map.put(k1, v1);
              map.put(k2, v1);

              map.get(k1); // returns v1
              map.get(k2); // also returns v1
              [/code]

              I'm sure I misunderstand the problem.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by JosAH
                Well, an ordinary Map can do the job then:

                [code=java]
                K k1= ...
                K k2= ...
                V v1= ...
                Map<K, V> map= ...

                map.put(k1, v1);
                map.put(k2, v1);

                map.get(k1); // returns v1
                map.get(k2); // also returns v1
                [/code]

                I'm sure I misunderstand the problem.

                kind regards,

                Jos
                I think I just lost my last mint sweet .

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  If I understand your problem correctly you need a compound key. The constituents
                  of the compound key are k1 and k2 in your examle.

                  Why not generalize it completely?

                  [code=java]
                  public interface Key { }

                  public class AtomicKey<K> implements Key {
                  private K key;
                  public AtomicKey<K>(K key) { this.key= key; }
                  public int hashCode() { return key.hashCode(); }
                  public boolean equals(Object obj) {
                  if (!(obj instanceof AtomicKey<K>)) return false;
                  AtomicKey<K> that= (AtomicKey<K>)o bj;
                  return key.equals(that .key);
                  }
                  }
                  public class CompoundKey<K> implements Key {
                  private Key k1;
                  private Key k2;
                  public CompoundKey(Key k1, Key k2) { this.k1= k1; this.k2= k2; }
                  public int hashCode() { return (k1.hashCode()< <1)^k2.hashCode (); }
                  public boolean equals(Object obj) {
                  if (!(obj instanceof CompoundKey<K>) ) return false;
                  CompoundKey<K> that= (CompoundKey<K> )obj;
                  return k1.equals(that. k1) && k2.equals(that. k2);
                  }
                  }
                  [/code]

                  This way you can compose your compound keys from simple keys any way
                  you want. Your map should store those compound keys (Key) and the values.

                  kind regards,

                  Jos

                  ps. that code was from the top of my head, so no guarantee whatsoever ;-)

                  Comment

                  • madhoriya22
                    Contributor
                    • Jul 2007
                    • 251

                    #10
                    Originally posted by JosAH
                    Well, an ordinary Map can do the job then:

                    [code=java]
                    K k1= ...
                    K k2= ...
                    V v1= ...
                    Map<K, V> map= ...

                    map.put(k1, v1);
                    map.put(k2, v1);

                    map.get(k1); // returns v1
                    map.get(k2); // also returns v1
                    [/code]

                    I'm sure I misunderstand the problem.

                    kind regards,

                    Jos
                    Thanks Jos for ur valuable suggestion..... ...

                    Comment

                    • madhoriya22
                      Contributor
                      • Jul 2007
                      • 251

                      #11
                      Originally posted by JosAH
                      If I understand your problem correctly you need a compound key. The constituents
                      of the compound key are k1 and k2 in your examle.

                      Why not generalize it completely?

                      [code=java]
                      public interface Key { }

                      public class AtomicKey<K> implements Key {
                      private K key;
                      public AtomicKey<K>(K key) { this.key= key; }
                      public int hashCode() { return key.hashCode(); }
                      public boolean equals(Object obj) {
                      if (!(obj instanceof AtomicKey<K>)) return false;
                      AtomicKey<K> that= (AtomicKey<K>)o bj;
                      return key.equals(that .key);
                      }
                      }
                      public class CompoundKey<K> implements Key {
                      private Key k1;
                      private Key k2;
                      public CompoundKey(Key k1, Key k2) { this.k1= k1; this.k2= k2; }
                      public int hashCode() { return (k1.hashCode()< <1)^k2.hashCode (); }
                      public boolean equals(Object obj) {
                      if (!(obj instanceof CompoundKey<K>) ) return false;
                      CompoundKey<K> that= (CompoundKey<K> )obj;
                      return k1.equals(that. k1) && k2.equals(that. k2);
                      }
                      }
                      [/code]

                      This way you can compose your compound keys from simple keys any way
                      you want. Your map should store those compound keys (Key) and the values.

                      kind regards,

                      Jos

                      ps. that code was from the top of my head, so no guarantee whatsoever ;-)

                      I am not getting this code.......may be a bit complex for me.........

                      can u tell me.........how can i use this compound key funda in my code......

                      just a hint to start with........... .Thanks

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by madhoriya22
                        I am not getting this code.......may be a bit complex for me.........

                        can u tell me.........how can i use this compound key funda in my code......

                        just a hint to start with........... .Thanks
                        You have a two "keys" which make one key. Lump these "keys" together into one object by creating a Key class that is composed of these two objects.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by r035198x
                          You have a two "keys" which make one key. Lump these "keys" together into one object by creating a Key class that is composed of these two objects.
                          spoilsport ...

                          kind regards,

                          Jos ;-)

                          Comment

                          • lhopeusa
                            New Member
                            • Jul 2007
                            • 1

                            #14
                            Works fine but I had to implement Comparable and add a compareTo method to use it in a TreeMap so the keys can be sorted.

                            Regards,
                            Larry

                            Originally posted by josAH
                            [code=java]
                            public class CompoundKey<K> implements Key {
                            private Key k1;
                            private Key k2;
                            public CompoundKey(Key k1, Key k2) { this.k1= k1; this.k2= k2; }
                            public int hashCode() { return (k1.hashCode()< <1)^k2.hashCode (); }
                            public boolean equals(Object obj) {
                            if (!(obj instanceof CompoundKey<K>) ) return false;
                            CompoundKey<K> that= (CompoundKey<K> )obj;
                            return k1.equals(that. k1) && k2.equals(that. k2);
                            }
                            }
                            [/code]

                            This way you can compose your compound keys from simple keys any way
                            you want. Your map should store those compound keys (Key) and the values.

                            kind regards,

                            Jos

                            ps. that code was from the top of my head, so no guarantee whatsoever ;-)
                            Last edited by JosAH; Jul 24 '07, 06:57 PM. Reason: corrected the quotes and code tags

                            Comment

                            Working...