1

I have 2 hashmap which contains a Key and a Value for both. My objective is to find out the duplicates in one of the hashmap and if a duplicate is detected , i will extract the KEY. May i know how can i do that in a hashmap?

basically the first hashmap stores the station ID and the 2nd hashmap stores the station name at some point there will be interception for changing train too. How do i code it in a way that if the user enters a boarding station on train line 1 and alighting station on train line 2, it would then return telling you that you ahve to make a change in train to get to the location which is location on the other train line.

Sorry about the explanation it, its kinda confusing.

Would be nice if you could help out! Thank you!

2
  • 2
    Post your code and we'll be able to help. Commented May 17, 2012 at 20:07
  • 1
    Please ask your question in proper english and explain the details of your problem. Until now it is not clear what you mean by a duplicate or what "extracting" the key could mean. Why you mention a arraylist in the title is not clear to me, please be more concrete on your problem. Commented May 17, 2012 at 20:10

2 Answers 2

1

If you are looking for duplicate keys in two different Maps, that's easy.

Set set1 = new SomeKindOfSet(map1.getEntrySet());  // makes a copy, important!
Set set2 = map2.getEntrySet();
set1.retainAll(set2);

However, if you are looking for duplicate values that's harder. There are some third-party "two way" Maps. But since I don't understand what you really want it's hard to say.

Sign up to request clarification or add additional context in comments.

Comments

0

You mean duplicate in the values, right? And I'll assume that means ALL values for all keys.

I'm sorry, but I think you might be using the wrong level of abstraction here. Don't contort your code to accomplish this with two hash maps. Create an abstration using an object - Java's an object-oriented language - that represents the thing you're trying to model and build an API to make that easy for users to deal with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.