Link to home
Start Free TrialLog in
Avatar of dlcnet
dlcnetFlag for United Kingdom of Great Britain and Northern Ireland

asked on

create from a list a key value map

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
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Instead of List use Hashmap ;Please post sample code
Avatar of dlcnet

ASKER

@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
Avatar of rrz
Your question isn't very clear. Please show us your code.
Post your code or try giving some concrete example.
Avatar of dlcnet

ASKER

@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

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.
Avatar of dlcnet

ASKER

@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!
ASKER CERTIFIED SOLUTION
Avatar of muktajindal
muktajindal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial