This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to merge two hashtables elements in another hashtable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to merge two hashtables elements in another hashtable?" Watch "How to merge two hashtables elements in another hashtable?" New topic
Author

How to merge two hashtables elements in another hashtable?

hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
How to merge two hashtables elements in another hashtable?

I have Two hashtable.

First hashtable with element key as '0' and value as 'hello'
key as '1' and value as 'new'

Second hashtable with element key as '0' and value as 'how are you'
key as '1' and value as 'phone'
key as '2' and value as 'something'

I need to merge these two hashtables values for the samekeys only in another hashtables.

Like Third hashtable, i need like this key as '0' and value as 'hello how are you'
key as '1' and value as 'new phone'

and Key as '2' should not merge, bec first hashtables doesnt contains tat key.
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

Iterate through First hashtable based on the KeySet
for each key from the firsthashtable get value from 2nd one. (Null means no value present)
concant the values as you required.
put it in the 3rd one.

copy the rest from 1st and 2nd hashtable to 3rd one.

I think this is what you expecting


-Chiru
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
Thanks for your reply..

I tried like this, I dont knoe how to get exactly...

package pack1;
import java.util.*;
public class Sample {
public static void main(String[] args){

Hashtable A = new Hashtable();
Hashtable B = new Hashtable();
Hashtable C = new Hashtable();

A.put("0", "hello");
A.put("1", "new");
A.put("2", "hi");
A.put("3", "Good Morning");

B.put("0", "how are you");
B.put("1", "phone");
B.put("3", "sir");

Set firstSet = A.keySet();
Iterator itr = firstSet.iterator();

while(itr.hasNext()){

}
}

}


I need to get output like:

In Hashtable c '0' as hello how are you and '1' as new phone and '2' as hi and '3' as GoodMorning Sir, if the samekeys elements in not present, then no need to merge simply add that element to new hashtable...
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Iterate second map, check if first map contains current key, if yes, concat value, if no, add value:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Zandis Murāns, it doesn't help people to learn if you simply give them an answer like that. Chiranjeevi Kanthraj had already given enough information, but by giving a complete answer rather than hints, you can prevent the original poster from learning from their problem.
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
Thanks, i ll try this.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Campbell Ritchie wrote:Zandis Murāns, it doesn't help people to learn if you simply give them an answer like that. Chiranjeevi Kanthraj had already given enough information, but by giving a complete answer rather than hints, you can prevent the original poster from learning from their problem.

No, I completely don't agree with you. Because of topic is "Beginning Java" they'd need to see clear and nice code, so they can inspect every line of code and see how it works.
I belive (and that is the reason I'm trying to supply them with such code snippets) they are not doing homeworks or something like that and just copy-pasting such code snippets without giving any attention to them.
Have a nice day anyways.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
I think we shall have to agree to disagree. But the policy here is that we do not give out such code. What we do, however, is to inspect the code shown and clean it up.
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
That depends how ppl are acceptin .. if i simply copy and without analyze, i ll be loser. but if i analyze, then it's fine..
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
In hashtables, the values am having is vectors.

First Hashtable --> {0=[1,2,7]}

Second Hashtable --> {0=[3,4,5,6]}

I need to get in my resultant

Third Hashtable --> {0=[1,2,7,3,4,5,6]},


but am getting like {0=[1,3]}, when i tried like this,

A.put(key,((Vector)A.get(key)).get(0)+","+ ((Vector) B.get(key)).get(0)); ....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
When you started off, you told us you had values which appeared to be Strings. Now you are telling us they are Vectors.

Why are you using Vector? You should usually use ArrayList for an array-based List implementation, although there are a few instances where a Vector is actually required. Now, ArrayList implements the List<T> interface (so does Vector), so you can go through the List interface here and here, where you can find methods to add elements of one list to another.
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
sorry it's my fault.. so, i need to convert vector into arraylist?.. is there some other way, by using vector itself to achieve my result.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Please read that he says using ArrayList might suit your requirement better. If you want to use in Vector only, then in a similar fashion you iterate your first Hashtable - get its Vector value and then get the Vector value of the second Hashtable and integrate both the vectors.

FirstVector:addAll(SecondVector) shall be used.


I am not able to understand what you are doing here. Are you deliberately adding a "," between the values? Seems like you are putting a String object rather than a Vector object into the Hashtable. It would be better for you to use Hashtable<String,Vector> to avoid confusion i think.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You should be declaring all your Lists as type List, and it becomes very easy to change the implementation type. Look:Now, you can write new Vector<>() or new ArrayList<>() or new LinkedList<>() in that first line, and you will not notice any difference in the rest of the code. You only need that one change. ArrayList is the most efficient for most uses, but LinkedList is faster in certain circumstances (eg adding and removing at the beginning of the List).
For similar flexibility, you should give your Map (actually you should use HashMap rather than Hashtable, which is regarded as a very old-fashioned almost legacy class) actual type parameters; I think you want
Map<String, List<Something>> map1 = new HashMap<>();
...

Again you can only use <> without something inside in Java7.
The reason to use ArrayList and HashMap is that they are more modern and give faster performance; it says in the API that ArrayList is often preferable. You may find some applications do require Vector, however, eg this constructor which takes Vector, rather than List.

Anyway, there is a method which does what I think you want; I think you will find that method in the Vector class and in the ArrayList class, since I could find it in the List interface.
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
In that code,
I tried to get all the values of first vector and second vector of those two hashtables. but didnt work.
I will try using arraylist.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I am not understanding which code you say is not working... I have not given any code for Vector... Hope you try ArrayList and resolve the issue ....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
How did you try to get those Lists? How did you put the Lists into the Map in the first place? What error are you suffering? Did you import thsoe classes correctly?
This is how you can do it, and if you write swap Vector/ArrayList or HashMap/Hashtable you will get the same end result. Only a few microseconds slower.You will obviously have to put something real in place of the ...

And have you found the method in the List interface which I think will do exactly what you want? It is much simpler than you think.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to merge two hashtables elements in another hashtable?