Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Hasmap having single key and mutiple value?

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to write Hashmap when key is loan id and values are person name and loan amount?that is one key and multiple value
 
Sheriff
Posts: 22208
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

priyanaka jaiswal wrote:please send me code


We are NotACodeMill.

The solution is pretty easy - instead of a HashMap<K,V>, where K is the key type and V is the value type, use a HashMap<K,List<V>> or a HashMap<K,Set<V>>. The insertion is also pretty easy:
- first get the value for the key which is a List<V> or Set<V>.
- if that does not exist, create a new one and add that to the map for the key.
- add to the List / Set.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't just hand out code like that round here, but we'll help you work out your own solution.

My suggestion would be: you need a single object that encapsulates all the data related to a loan that you can add as the value. That suggests you define your own class to do that.

Edit: And welcome to The Ranch!
 
Ranch Hand
Posts: 451
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Use a bean to store multiple values (Loan ID, person name, loan amount e.t.c).

Key - Loan ID
Value - LoanNean (loanID, personName, loanAmount)
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you dont want to code it yourself and have no aversion to existing libraries you can use the MultiMap implementation in Google's Collections library (now called guava):

http://code.google.com/p/guava-libraries/
http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html
http://www.factorypattern.com/multimap-in-google-collections-library/
 
reply
    Bookmark Topic Watch Topic
  • New Topic