• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

HashMap in Java

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to print the result if the value I entered is containing in the HashMap.

For example,
3         <- case
uncle sam <- input
111222
tom
222333
harry
333444
uncle sam <- queries
tom tom
harry

uncle sam=111222 <- output
Not found
harry=333444

The output looks like above.



However, a message, not found, is not printed when I entered the value that is not containing in HashMap.
How can I modify the code?

If I use else statement in the second for statement then the message is printed by the number of loop.
 
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove if statement out of second for loop. Before this loop create variable When condition if (key.contains(query)) gives true you have to print key value pair,
make found true and use keyword break to exit loop.
 
Jiyoung Kim
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Spencers wrote:Remove if statement out of second for loop. Before this loop create variable When condition if (key.contains(query)) gives true you have to print key value pair,
make found true and use keyword break to exit loop.



It works correctly.
I set boolean = found as a false outside of second for loop, but it doesn't seem to be used properly.



I just declared new variables for key and value, and checked if the query value equals to it.
It works, so I guess that boolean value is not necessary to use. However, if I set boolean outside of main method with static, it might be used, but I'm not sure if I still need it.
 
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something not quite right about your using a loop to find entries in a Map. A HashMap is designed to store a key and a value, so you can enter

Campbell Ritchie
385738

and the Map will contain a Map.Entry object representing [Campbell Ritchie ↦ 385738]. What if you want a different Ritchie?  [David Ritchie ↦ 385460] That isn't how Maps are supposed to work. Consider a different data representation. You can have a Map<String, List<String>> instead. You can split the name into two parts and have a mapping like this:-

[Campbell Ritchie ↦ [385738]]
[David Ritchie ↦ [385460]]
[Campbell ↦ [385738]]
[David ↦ [385460]]
[Ritchie ↦ [385738, 385460]]

Now you can use Ritchie or Campbell or David to find the phone numbers; you will also get both phone numbers for both Ritchies. Read more about Maps in the Java™ Tutorials; there may be an example similar to what I am describing.
 
Shiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!