Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

create from a list a key value map

Posted on 2008-10-20
8
Medium Priority
?
1,138 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 28

Expert Comment

by:rrz
ID: 22763679
Your question isn't very clear. Please show us your code.
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

721 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