JohnDotOwl
JohnDotOwl

Reputation: 3755

Java HashMap with arraylist

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!

Upvotes: 1

Views: 160

Answers (2)

user949300
user949300

Reputation: 15729

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.

Upvotes: 1

duffymo
duffymo

Reputation: 308998

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.

Upvotes: 0

Related Questions