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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:20:32+00:00 2026-05-24T02:20:32+00:00

I want to have a map that permits a tree-like behaviour. I want to

  • 0

I want to have a map that permits a tree-like behaviour. I want to be able to define a Map from string to an object, that can be another map, or a string:

    Map<String,(String OR Map)>

The only way I think I know how to do it is through the Visitor Pattern. Is there an already made data structure that implements this?

UPDATE:
Here is the context:
I want to parse form submitted through a post request. The form is multilevel and can have an arbitrary number of fields(chosen by the user). What I decided to do is to name the fields in the form with numbers as such:

  1. User|name
  2. processor|1|speed
  3. processor|1!name
  4. processor|2|speed
  5. disk|1|name
  6. disk|2|name

I decided to break it up at | and then create a tree like structure so itwould look something like this:

  • User
    • name = whatever the user inputs
  • Processor
    • 1
      • speed = whatever the user inputs
      • name = whatever the user inputs
    • 2
      • speed = whatever the user inputs
  • Disk
    • 1
      • name = whatever the user inputs
    • 2
      • name = whatever the user inputs

Only the last leaves would contain strings. There is not that many fields so I had a feeling using a Map may be a bit of an overkill, but honestly I really didn’t know how to do this with anything else.

  • 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-24T02:20:33+00:00Added an answer on May 24, 2026 at 2:20 am

    Introduce an object that represents String or Map. Presumably you use them in a way that has some shared behaviour? This could be abstracted to a common base class, and it sounds a little like the composite pattern (tree-like behaviour). For example:

    interface StructuredObject {}
    
    class Leaf implements StructuredObject {}
    
    class Composite implements StructuredObject {}
    
    Map<String, StructuredObject> map = ...;
    

    As you point out, you can then use the visitor pattern to traverse through the map and avoid type casting.

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

Sidebar

Related Questions

I have a map that i want to update from a separate thread. Im
I have a map that represents a DB object. I want to get 'well
I have a Map, that I want to persist. The domain object is something
I have one very general object that I want to map to a destination
i have a map (just an image) and i want that a user can
I want to have a map view that loads from an image that I
I have an STL map that I want to iterate through, and can't seem
I have Java Map (out of Strings and Ints) objects that I want to
I have a silverlight application that contains Bing map. I want when the user
I have map.resources :posts and I want to be able to serve post bodies

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.