File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How we compare two maps in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How we compare two maps in java " Watch "How we compare two maps in java " New topic
Author

How we compare two maps in java

Anitha Kuchana
Greenhorn

Joined: Dec 22, 2011
Posts: 21
My data present in list.That list is a value of MAP.same another MAP also containing list.Now i want to compare list values.How we compare list values of Map data structure. Please any one help me?
Raghav Viswanathan
Greenhorn

Joined: Apr 26, 2012
Posts: 26

Hello Anitha,
Could you please post a code sample of the objects that you have to compare. The post doesnt seem to be that clear.

Thanks,
Raghav V


Better late than never.
Anitha Kuchana
Greenhorn

Joined: Dec 22, 2011
Posts: 21
i am using struts with hibernate.in map code is key=1 value={place Code= 1001,place name= ""}and key=2 value={place code=1002 ,place name=""}....etc
And another map also having same values Now i want to compare the place code.how we compare this
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4679
    
    7

This is a common anti-pattern that I have the misfortune to see too many times. A map that contains maps that map object properties as key-value pairs. It's a very bad practice and rather than try to figure out the answer to your original question, I suggest you choose a different data structure. Java is an object-oriented language, so what you should have is, at the least, a List of objects that encapsulate the values of 'code', 'name', etc. and an implementation of equals() for that kind of object. Then you can use the built-in List methods to check if an element of one List has an equivalent element in another List.


Junilu - [How to Ask Questions] [How to Answer Questions]
Anitha Kuchana
Greenhorn

Joined: Dec 22, 2011
Posts: 21
thank you for your answer .
can you give me the code for :convert map to list
please give a sample code ...?
example :
i am using struts with hibernate in my project.

Map<Integer, pojo name > oldMap = null;
Map<Integer, pojo name > newMap = null;

oldMap ={
1,values={list of objects that each object contains {place code="1001" placename="USA"}}
2,values={list of objects that each object contains {place code="1001" placename="USA"}...etc
}

similarlly

newMap={1,values={list of objects that each object contains {place code="1001" placename="USA"}}
2,values={list of objects that each object contains {place code="1001" placename="USA"}...etc
}


Now i want compare these place code of oldMap and newMap..

earlier you said convert map into list....
please give me the sample code.And how to convert from map into list?
And solution for the above situation?



Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4679
    
    7

Sorry, but this site is NotACodeMill (<--click). You'll have to ShowSomeEffort and give it your best shot to do it yourself. Since you're using Hibernate, you should read up on Object-Relational Mapping concepts and how to map (translate) relational data into objects. Hibernate can actually do a lot of the heavy lifting for you here.

Edit: missed the fact that you asked for examples. You can find examples in the Hibernate documentation: Start here: http://docs.jboss.org/hibernate/orm/ then choose the version of Hibernate that you're using, go to reference or manual or quickstart (depends on the version), then pick a language and format. Good luck.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Anitha Kuchana wrote:thank you for your answer .
can you give me the code for :convert map to list

Sure. Iterate through the Map "values", and then iterate through each value in its list, and add them to a standard List.

If you want better advice than that, then you'll have to give us a LOT more information about what it is you want to do. All you've described is the mechanics; and therefore you have a mechanical answer.

please give a sample code ...?

No...and I think Junilu's explained why.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How we compare two maps in java