Solved

create from a list a key value map

Posted on 2008-10-20
8
1,133 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 27

Expert Comment

by:rrz
ID: 22763679
Your question isn't very clear. Please show us your code.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
configure dependency in POM for new database 3 34
designing in object programming 12 95
How do I remove an object from a 3 40
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
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…

807 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