开发者

How to "join" Hashtables in Java?

开发者 https://www.devze.com 2022-12-27 12:45 出处:网络
I have two strings: A { 1,2,3,4,5,6 } B { 6,7,8,9,10,11 } it doesnt really matter what the numbers are in the strings. So then the user is going to pick what to join:

I have two strings:

A { 1,2,3,4,5,6 } B { 6,7,8,9,10,11 }

it doesnt really matter what the numbers are in the strings. So then the user is going to pick what to join:

A hashjoin A.a1 = B.b5 B

I think I put the A into a hashtabl开发者_如何转开发e by the A.a1 as the key and then iterate through B? The keys will be what the user wants then to join on and the data will be whats in the strings.


Are you sure you're trying to join hashtables? Perhaps you have the wrong data structure?

Look into java.util.Set (and java.util.HashSet). If you want the items that are in both tables, then it's a simple Set operation like so:

Collection A = new ...
...fill the A up...
Collection B = new ...
...fill the B up...
Set join = new HashSet();
join.addAll(A);
join.retainAll(B);

If you mean something more like a SQL table join, then the output will depend on what type of join you mean to perform, and what the equals sign means in this case. Note you'll have to write a Pair class (which you should make more descriptive than Pair for your exact case)

For a full join:

ArrayList pairs = new ArrayList();
for (Number numberA : A) {
  for (Number numberB : B) {
    pairs.add(new Pair(numberA, numberB));
  }
}

For a full join with a where clause:

ArrayList pairs = new ArrayList();
for (Number numberA : A) {
  for (Number numberB : B) {
    if (check the condition of the where clause here) {
      pairs.add(new Pair(numberA, numberB));
    }
  }
}

That's about as good an answer that can be given under the circumstances, as your question isn't very specific. If these general answers don't help you out, then you'll need to explain your question in more detail to get a more detailed answer.

--- First Edit, after some clarification ---

Ok, so it's an SQL-like equi-join.

Hashtables are Maps, which means they have an element in one "domain" which can be used to look up an element in another "domain". In a hash table, the first domain is the set of keys, and the second domain is the set of values. Think of it as a bunch of labels and a bunch of items. If the equi-join is to be performed, it must join like elements. That means it will either join one key to another key, or it will join one value to another value.

For keys:

Hashtable A = ...
Hashtable B = ...

Set keyJoin = new HashSet();
keyJoin.addAll(A.keySet());
keyJoin.retainAll(B.keySet());

For values:

Hashtable A = ...
Hashtable B = ...

Set valueJoin = new HashSet();
valueJoin.addAll(A.values());
valueJoin.retainAll(B.values());

It doesn't make sense to join the hashtables themselves; because, one "matching" value may live in both hashtables but be referenced by two different keys. Likewise, one "matching" key found in two different hashtables might not refer to the same value.


Your question doesn't make much sense. A hashtable (or hashmap), stores data as keys and values. You've said nothing about which of those values should be keys, and which should be values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号