• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How we compare two maps in java

 
Anitha Kuchana
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anitha Kuchana
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7749
61
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anitha Kuchana
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7749
61
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic