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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:34:51+00:00 2026-05-27T03:34:51+00:00

I need to create a Hashmap of field/values contained in an Entity, so I

  • 0

I need to create a Hashmap of field/values contained in an Entity, so I can can use them to replace them in a String containing tags with the field names.

I have this code:

public static String replaceTags(String message, Map<String, String> tags) ...

Which replaces all tags found in message for the equivalent values in tags, but in order to build the Map table I need to take “any” Entity and be able to create a Map from the Entity. So, how could I make that possible? to get a routine where I send the Entity and get as return a Map with all the fields and values.

public static Map<String, String> getMapFromEntity(Object entity){
    Map<String, String> map = new HashMap<String, String>();

    ...?????

    return map;
}

I know I could use reflection and this is the only approach I have found to get this done, but is there any other way to accomplish the same?, I mean a more efficient way.

Thanks.

  • 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-05-27T03:34:51+00:00Added an answer on May 27, 2026 at 3:34 am
        Field[] fields = entity.getClass().getFields();
        Map<String, String> map = new HashMap<String, String>();
        for(Field f : fields)
                map.put(f.getName(),(String) f.get(entity));
    

    O, and your entity should be an object of your class, not your class itself.
    If your fields are private and you have getters for them, you should use getMethods() and check if method name starts with “get” prefix.Like this:

        Method[] methods = entity.getClass().getMethods();
        Map<String, String> map = new HashMap<String, String>();
        for(Method m : methods)
        {
            if(m.getName().startsWith("get"))
            {
                String value = (String) m.invoke(entity);
                map.put(m.getName().substring(3), value);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a dictionary/hashmap where the Keys are enums Values are some
I need to create an empty map. if (fileParameters == null) fileParameters = (HashMap<String,
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
I have this field: HashMap<String, HashMap> selects = new HashMap<String, HashMap>(); For each Hash<String,
What's the best way to create a bean for a HashMap <Class,String >? Specifically,
I need to store a TDSContent into a hashmap specifying a String key. I
I'm building a hashmap class that can have string keys and ints, bools, strings
I need create a server side game loop, the problem is how to limit
I need create custom dialog and put JPanel into it. Is it possible?

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.