Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create from a list a key value map

Posted on 2008-10-20
8
Medium Priority
?
1,139 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
ID: 22757167
Instead of List use Hashmap ;Please post sample code
0
 

Author Comment

by:dlcnet
ID: 22757258
@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 28

Expert Comment

by:rrz
ID: 22763679
Your question isn't very clear. Please show us your code.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:muktajindal
ID: 22765800
Post your code or try giving some concrete example.
0
 

Author Comment

by:dlcnet
ID: 22766076
@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
ID: 22766149
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
ID: 22766365
@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 2000 total points
ID: 22783500
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

783 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