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
13 1617
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.
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.....
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
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>
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:- -
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:- -
-
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...hope u would understand what I am trying to say here...Thanks
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: -
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
-
I'm sure I misunderstand the problem.
kind regards,
Jos
Well, an ordinary Map can do the job then: -
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
-
I'm sure I misunderstand the problem.
kind regards,
Jos
I think I just lost my last mint sweet .
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? -
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>)obj;
-
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);
-
}
-
}
-
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 ;-)
Well, an ordinary Map can do the job then: -
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
-
I'm sure I misunderstand the problem.
kind regards,
Jos
Thanks Jos for ur valuable suggestion........
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? -
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>)obj;
-
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);
-
}
-
}
-
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
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.
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 ;-)
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 -
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);
-
}
-
}
-
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 ;-)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
...
|
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...
|
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...
|
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;
....
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
| |