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
Collections
Collapse
X
-
Tags: None
-
Originally posted by r035198xSee the entry for Map.
That means you have to make your own Map.
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
-
Originally posted by madhoriya22Sorry if I misunderstood your suggestion.....
<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,
JosComment
-
Originally posted by JosAHI 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
<crosses fingers>Comment
-
Originally posted by JosAHI 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
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; }
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); } } }
Comment
-
Originally posted by r035198xI bet my last mint sweet that he means he wants to be able to supply k1 or k2.
<crosses fingers>
[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,
JosComment
-
Originally posted by JosAHWell, 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,
JosComment
-
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
-
Originally posted by JosAHWell, 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,
JosComment
-
Originally posted by JosAHIf 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........... .ThanksComment
-
Originally posted by madhoriya22I 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........... .ThanksComment
-
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 ;-)Comment
Comment