Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to merge two hashtables elements in another hashtable?

 
hariharan jayaprakash
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Hibernate Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
hariharan jayaprakash
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java Java ME Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterate second map, check if first map contains current key, if yes, concat value, if no, add value:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, i ll try this.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic