473,461 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Collections

madhoriya22
252 100+
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
Jul 10 '07 #1
13 1620
r035198x
13,262 8TB
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.
Jul 10 '07 #2
madhoriya22
252 100+
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.....
Jul 10 '07 #3
JosAH
11,448 Expert 8TB
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
Jul 10 '07 #4
r035198x
13,262 8TB
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>
Jul 10 '07 #5
madhoriya22
252 100+
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...Here is the method:-
Expand|Select|Wrap|Line Numbers
  1. public Hashtable getWorkPackageSummary(String workPackageId) {
  2.  
  3.         Connection con = null;
  4.         ResultSet rs = null;
  5.  
  6.         Hashtable hsht = new Hashtable();
  7.         String status = null;
  8.         try {
  9.             con = new MySqlDAOFactory().getConnection();
  10.             PreparedStatement pStatement = null;
  11.             pStatement = con.prepareStatement(GET_WORKPACKAGE_SUMMARY);
  12.             System.out.println(GET_WORKPACKAGE_SUMMARY);
  13.  
  14.             pStatement.setString(1, workPackageId);
  15.             System.out.println("Id:::"+workPackageId);
  16.  
  17.             rs = pStatement.executeQuery();
  18.             while(rs.next()) {                
  19.  
  20.                 WorkPackageSummaryVO workPackageSummary = new WorkPackageSummaryVO();
  21.  
  22.                 workPackageSummary.setWorkPackageName(rs.getString("WORKPACKAGE_NAME"));
  23.                 status = rs.getString("CURRENT_DEFECT_STATUS");
  24.                 workPackageSummary.setCount(rs.getInt("COUNT"));
  25.  
  26.                 if(workPackageSummary != null) {
  27.                     hsht.put(status, workPackageSummary);
  28.                 }
  29.             }
  30.             rs.close();
  31.             pStatement.close();
  32.         }catch (Exception ex) {
  33.             System.out.println("Exception occured while retriving WorkPackage Summary from database " + ex);
  34.             ex.printStackTrace();
  35.         }finally {
  36.             try {
  37.                 con.close();
  38.             } catch (SQLException ex) {
  39.                 System.out.println("Exception occured while closing connection during WorkPackage Summary Retrival " + ex);
  40.                 ex.printStackTrace();
  41.             }
  42.         }
  43.         return hsht;
  44.     }
  45.  
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:-
Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i = 0; i < vct1.size(); i++) {
  3.                 System.out.println("for i = "+i);
  4.                 pStatement.setString(1, (String)vct1.get(i));
  5.                 //System.out.println("Id:::"+workPackageId);
  6.  
  7.                 rs = pStatement.executeQuery();
  8.                 while(rs.next()) {
  9.                     System.out.println("Inside while loop");
  10.  
  11.                     WorkPackageSummaryVO workPackageSummary = new WorkPackageSummaryVO();
  12.  
  13.                     status = rs.getString("CURRENT_DEFECT_STATUS");
  14.                     workPackageSummary.setCurrentDefectStatus(rs.getString("WORKPACKAGE_NAME"));
  15.                     workPackageSummary.setCount(rs.getInt("COUNT"));
  16.  
  17.                     if(workPackageSummary != null) {
  18.                         hsht.put(status, workPackageSummary);
  19.                     }
  20.                 }
  21.             }
  22.  
I cant get this information through query...bcoz it is group by status...It is such a long description...hope u would understand what I am trying to say here...Thanks
Jul 10 '07 #6
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. K k1= ...
  2. K k2= ...
  3. V v1= ...
  4. Map<K, V> map= ...
  5.  
  6. map.put(k1, v1);
  7. map.put(k2, v1);
  8.  
  9. map.get(k1); // returns v1
  10. map.get(k2); // also returns v1
  11.  
I'm sure I misunderstand the problem.

kind regards,

Jos
Jul 10 '07 #7
r035198x
13,262 8TB
Well, an ordinary Map can do the job then:

Expand|Select|Wrap|Line Numbers
  1. K k1= ...
  2. K k2= ...
  3. V v1= ...
  4. Map<K, V> map= ...
  5.  
  6. map.put(k1, v1);
  7. map.put(k2, v1);
  8.  
  9. map.get(k1); // returns v1
  10. map.get(k2); // also returns v1
  11.  
I'm sure I misunderstand the problem.

kind regards,

Jos
I think I just lost my last mint sweet .
Jul 10 '07 #8
JosAH
11,448 Expert 8TB
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?

Expand|Select|Wrap|Line Numbers
  1. public interface Key { }
  2.  
  3. public class AtomicKey<K> implements Key {
  4.    private K key;
  5.    public AtomicKey<K>(K key) { this.key= key; }
  6.    public int hashCode() { return key.hashCode(); }
  7.    public boolean equals(Object obj) {
  8.       if (!(obj instanceof AtomicKey<K>)) return false;
  9.       AtomicKey<K> that= (AtomicKey<K>)obj;
  10.       return key.equals(that.key);
  11.    }
  12. }
  13. public class CompoundKey<K> implements Key {
  14.    private Key k1;
  15.    private Key k2;
  16.    public CompoundKey(Key k1, Key k2) { this.k1= k1; this.k2= k2; }
  17.    public int hashCode() { return (k1.hashCode()<<1)^k2.hashCode(); }
  18.    public boolean equals(Object obj) {
  19.       if (!(obj instanceof CompoundKey<K>)) return false;
  20.       CompoundKey<K> that= (CompoundKey<K>)obj;
  21.       return k1.equals(that.k1) && k2.equals(that.k2);
  22.    }
  23. }
  24.  
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 ;-)
Jul 10 '07 #9
madhoriya22
252 100+
Well, an ordinary Map can do the job then:

Expand|Select|Wrap|Line Numbers
  1. K k1= ...
  2. K k2= ...
  3. V v1= ...
  4. Map<K, V> map= ...
  5.  
  6. map.put(k1, v1);
  7. map.put(k2, v1);
  8.  
  9. map.get(k1); // returns v1
  10. map.get(k2); // also returns v1
  11.  
I'm sure I misunderstand the problem.

kind regards,

Jos
Thanks Jos for ur valuable suggestion........
Jul 10 '07 #10
madhoriya22
252 100+
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?

Expand|Select|Wrap|Line Numbers
  1. public interface Key { }
  2.  
  3. public class AtomicKey<K> implements Key {
  4.    private K key;
  5.    public AtomicKey<K>(K key) { this.key= key; }
  6.    public int hashCode() { return key.hashCode(); }
  7.    public boolean equals(Object obj) {
  8.       if (!(obj instanceof AtomicKey<K>)) return false;
  9.       AtomicKey<K> that= (AtomicKey<K>)obj;
  10.       return key.equals(that.key);
  11.    }
  12. }
  13. public class CompoundKey<K> implements Key {
  14.    private Key k1;
  15.    private Key k2;
  16.    public CompoundKey(Key k1, Key k2) { this.k1= k1; this.k2= k2; }
  17.    public int hashCode() { return (k1.hashCode()<<1)^k2.hashCode(); }
  18.    public boolean equals(Object obj) {
  19.       if (!(obj instanceof CompoundKey<K>)) return false;
  20.       CompoundKey<K> that= (CompoundKey<K>)obj;
  21.       return k1.equals(that.k1) && k2.equals(that.k2);
  22.    }
  23. }
  24.  
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
Jul 10 '07 #11
r035198x
13,262 8TB
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.
Jul 10 '07 #12
JosAH
11,448 Expert 8TB
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 ;-)
Jul 10 '07 #13
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

Expand|Select|Wrap|Line Numbers
  1. public class CompoundKey<K> implements Key {
  2.    private Key k1;
  3.    private Key k2;
  4.    public CompoundKey(Key k1, Key k2) { this.k1= k1; this.k2= k2; }
  5.    public int hashCode() { return (k1.hashCode()<<1)^k2.hashCode(); }
  6.    public boolean equals(Object obj) {
  7.       if (!(obj instanceof CompoundKey<K>)) return false;
  8.       CompoundKey<K> that= (CompoundKey<K>)obj;
  9.       return k1.equals(that.k1) && k2.equals(that.k2);
  10.    }
  11. }
  12.  
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 ;-)
Jul 24 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: njp | last post by:
BlankHi, How do I create a tightly coupled Object 1 such that when I update it in one collection, it is simultaneously and automatically updated in other collections? The collections are defined...
1
by: Tim T. | last post by:
I'm currently working on a report to forecast production for finished goods. The user can select one or more items to forecast. In addition, they may select one or more warehouses to view...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
4
by: nhmark64 | last post by:
Hi, Does System.Collections.Generic.Queue not have a Synchronized method because it is already in effect synchronized, or is the Synchronized functionality missing from...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
5
by: WebSnozz | last post by:
Some collections are such that efficient search algorithms work on them such as binary search if the collection is a type which is sorted. I'm wondering how LINQ searches these collections and if...
2
by: Fred Heida | last post by:
Hi, i'm trying to (using managed C++) implment the IEnumerable<Tinterface on my class.. but have a problem with the 2 GetEnumerator method required.... what i have done is... ...
4
by: Sid Price | last post by:
Hello, I have a class of objects (Device) that are managed by another object (Devices) with a collection class (DeviceCollection) inherited from Collections.Hashtable. Each of the Device objects...
5
by: Michi Henning | last post by:
I can pass a generic collection as ICollection<Tjust fine: static void flatCollection(ICollection<intc) {} // ... List<intl = new List<int>(); flatCollection(l); // Works fine Now I...
3
by: Marco Shaw | last post by:
I've got some C# code to create a custom PowerShell cmdlet with these statements: .... using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; .... ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.