Solved

create from a list a key value map

Posted on 2008-10-20
8
1,135 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

732 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