Solved

create from a list a key value map

Posted on 2008-10-20
8
1,130 Views
Last Modified: 2013-11-23
Hi experts!

I have a class who returns me a List of options. The problem is that I would like to convert it somehow with a help of a method to a value - key object.
I am using this list in a JSP site and I would like to save at the very end the key of the selected option

Thanks
0
Comment
Question by:dlcnet
8 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
Comment Utility
Instead of List use Hashmap ;Please post sample code
0
 

Author Comment

by:dlcnet
Comment Utility
@chaituu

I have a hashmap already and a list too. My problem is how to link those too that the list values corresponds to the hashmap values and the result of the method is the key of the choosed value form the list
0
 
LVL 27

Expert Comment

by:rrz
Comment Utility
Your question isn't very clear. Please show us your code.
0
 
LVL 5

Expert Comment

by:muktajindal
Comment Utility
Post your code or try giving some concrete example.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dlcnet
Comment Utility
@all:

The code is bellow. I am using on the website the List method to generate a drop down list. I am interested to convert the option that the user converted into the Integer Key of the HashMap and the method is returning me, for example when user selected Bla Bla -> 1


public List listAll() {

   List options = new ArrayList<String>();

   options.add("Bla Bla");

   options.add("Bla Bla 2");

}
 

public Map<Integer, String> getOptions() {

   Map<Integer, String> optionsConvertor = new HashMap<Integer, String>();

   optionConvertor.put(1, getText("Bla Bla"));

   optionConvertor.put(2, getText("Bla Bla 2"));

}

Open in new window

0
 
LVL 5

Expert Comment

by:muktajindal
Comment Utility
As I understand, you already have this List and this Hashmap, and you want to get "1" when user selects "Bla Bla", "2" when user selects "Bla Bla 2".

You can achieve this by inverting the type specification in your map. Use Map<String, Integer> instead of Map<Integer, String> and then use optionsConvertor.get(key) to get the desired value.
0
 

Author Comment

by:dlcnet
Comment Utility
@muktajindal

yes, you understood corectly. Can you please please provide me a code who will do this?  I mean a method who gets as parameters the List and the Map and returns me the ineteger value of the key

Thanks!
0
 
LVL 5

Accepted Solution

by:
muktajindal earned 500 total points
Comment Utility
As I mentioned, you need to use Map<String, Integer> instead of Map<Integer, String>.
Refer to the attached code snippet. Using this, you can use it as follows :
optionsConvertor.get("Bla Bla") will return you 1.
optionsConvertor.get("Bla Bla 2") will return you 2.

public Map<String, Integer> getOptions() {

   Map<String, Integer> optionsConvertor = new HashMap<String, Integer>();

   optionsConvertor.put("Bla Bla", 1);

   optionsConvertor.put("Bla Bla 2", 2);

   return optionsConvertor;

}

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now