Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:59:03+00:00 2026-06-15T03:59:03+00:00

I am trying to populate a dropdown . I have a hashmap, from which

  • 0

I am trying to populate a dropdown . I have a hashmap, from which i am getting key and value. I have a bean from which i am getting a string value.
Now i want to populate the dropdown like this :

If the bean value is equal to hashmap key, then i have to make the key and value as “selected” in the dropdown otherwise, the string “plz select a value” must become the default drop down selected element and other key value pairs must come after it. If the bean value is equal to hashmap key, then they must not be repeated again.

This is what i have till far :

<%
String defaultText = "Please select a value";
while (iterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) i.next();
// getKey Method of HashMap access a key of map
String keyValue = (String) mapEntry.getKey();
//getValue method returns corresponding key's value
String value = (String) mapEntry.getValue();

%>

<option  selected="selected" value="none">
<%
if( beanNo!=null && beanNo.equals(keyValue))
{
%>
<%= beanNo %> , <%= value %>
<%
}
else
{
%>
<%= defaultText %>
<%
}
%>
</option>
<option  value="">
<%= value %> , <%= keyValue %>
</option>
<%
}
%>

However, this is not giving me the desired result. If the bean value is equal to hashMap keyValue, then, the same value is coming up twice and the defaultText is not coming at all .

Where am i going wrong ? Kindly help ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:59:04+00:00Added an answer on June 15, 2026 at 3:59 am

    This should work better :

    <%
        String defaultText = "Please select a value";
    %>
    <option value="none"><%= defaultText %></option>
    <%
        while (iterator.hasNext())
        {
            Map.Entry mapEntry = (Map.Entry) i.next();
            // getKey Method of HashMap access a key of map
            String keyValue = (String) mapEntry.getKey();
            //getValue method returns corresponding key's value
            String value = (String) mapEntry.getValue();
    %>
    <option <%= (beanNo!=null && beanNo.equals(keyValue)) ? "selected=\"selected\"" : "" %> value="<%= value %>">
    <%= keyValue %>
    </option>
    <%
        }
    %>
    

    Note : it has not been tested nor verified for syntax, maybe there are some minor errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to populate the second dropdown from my choice in the second,
I am trying to get the selected option from a dropdown and populate another
I have been trying to find out how to populate a dropdown box in
So I'm trying to populate a HTML dropdown using Javascript/Jquery. I'd like each <option>
I am trying to populate a dropdown list from database. In my view file
I'm trying to populate a dropdown list in my web page from a mysql
I am trying to populate a dropdown box using the selection from another dropdown
I am trying to populate the following dropdown list with information from MySQL database.
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a dropdown list when some other drop down gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.