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 7742671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:20:03+00:00 2026-06-01T09:20:03+00:00

when I passed a Map<String,String> in velocity template file, and when try to print

  • 0

when I passed a Map<String,String> in velocity template file, and when try to print the values of map it get sorted (on the basis of ASCII values).

I am doing as follows:

this is my velocity template file::

#set($tocList=${mapReference.mapValue})
#set($tocEntry="")

<div >
 #foreach($tocEntry in $tocList.keySet())
  <a href="#$tocEntry">$tocList.get($tocEntry)</a><br/>
 #end
</div>

My Java code is :

 Map<String, String> map=new HashMap<String, String>();
 Map<String,HashMap> m1=new HashMap<String, HashMap>();

   \\values that we want to print in template file

   map.put("sdfhfg", "Df lm"); 
   map.put("chdgfhd", "gBc Jk");
   map.put("dghjdhdf", "gI Ml");

   m1.put("mapValue", (HashMap) map);

  VelocityEngine velocityEngine = VelocityEngineFactory.getVelocityEngine();
  VelocityContext context = new VelocityContext();
  context.put("img",model);
  context.put("mapReference",m1);
  context.put("iterator", new IteratorTool());


  Template t = velocityEngine.getTemplate("tocTemplate.vm");
  StringWriter writer = new StringWriter();
    t.merge(context , writer);
    System.out.println(writer);

Output is:

 <div>
   <a href="#dghjdhdf">gI Ml</a><br/>
   <a href="#chdgfhd">gBc Jk</a><br/>
   <a href="#sdfhfg">Df lm</a><br/>
 </div>

Why these values get sorted? I want to print map as it is.

  • 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-01T09:20:05+00:00Added an answer on June 1, 2026 at 9:20 am

    The order of the HasMap will not remain constant over time, from Javadoc:

    This class makes no guarantees as to the order of the map; in
    particular, it does not guarantee that the order will remain constant
    over time.

    To remain the order, you can consider use LinkedHashMap as following suggested:

    This linked list defines the iteration ordering, which is normally the
    order in which keys were inserted into the map

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

Sidebar

Related Questions

So I have a Map that has some values in it being passed into
(Redefinition of the method public Collection<String> values(); ) I have a Map contained in
The string passed to my custom function is the following: SELECT key FROM ubis
I am being passed a series of keyvaluepair<string, uint> pairs, where the string represents
I am trying to do the following: boost::unordered_map<boost::flyweight<std::string>, boost::flyweight<std::string> > map; boost::flyweight<std::string> foo(name); map[foo]
I know how to map a list to a string: foostring = ",".join( map(str,
I have a DWR action with a method signature as follows: String joinGroup(String groupId,
I'm trying to get Fluent NHibernate 1.0 RTM to map a User entity for
Given the following code: static member private getIntValue (_map:Map<string, int>) (_key:string) = if (_map.ContainsKey
Say I had a template function that calculates the sum of a map's elements

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.