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

The Archive Base Latest Questions

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

How do I loop over a string of item and create an object based

  • 0

How do I loop over a string of item and create an object based on that?

I currently have this code:

public static Object ParseParams(String string)
{
    Object params = new Object();
    String[] lines = string.split("\n");
    for(String line : lines)
    {
        String[] splittedLine = line.split("=");
        params[splittedLine[0]] = splittedLine[1]; //JavaScript syntax, not Java!
    }
    return params;
}

The input string is in this format:

param1=value1
param2=value2
foo=bar

How do I fix the problematic line?

Edit

Sometimes the string would look like this:

foo=bar
param=1=hello
param=2=world

Would it be possible with Maps in Java to get the output like this:

foo
  bar
param
  1
    hello
  2
    world

So the Maps are sometimes nested, and it you would retrieve hello by calling params.get("param").get("1");

  • 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-26T13:32:24+00:00Added an answer on May 26, 2026 at 1:32 pm

    It sounds like you want either a Map, or a JSON library.

    Maps

    public static Map<String, String> ParseParams(String string)
    {
        Map<String, String> params = new HashMap<String, String>();
        String[] lines = string.split("\n");
        for(String line : lines)
        {
            String[] splittedLine = line.split("=");
            params.put(splittedLine[0], splittedLine[1]);
        }
        return params; // Get a param with params.get(key);
    }
    

    In general you’ll want a HashMap (fast but not stored in order), but there’s also a TreeMap which is slightly slower but stored in order (which can be useful sometimes).

    JSON

    The format JavaScript uses for objects is used as a general-purpose storage format called JSON (JavaScript Object Notation). This is only useful for storage / printing / network transmission. Internally, Java JSON libraries use maps (JavaScript interpreters probably do too).

    There are several Java APIs, but StackOverflow users seem to recommend Json-lib:

    public static JSONObject ParseParams(String string)
    {
        // Note that we need everything from the other method anyway
        return JSONObject.fromObject(ParseParams(string));
    }
    

    EDIT:

    You’re already reaching the point where using a Map is strained. I’d suggest just using a class:

    class MyStuff {
        String foo;
        Map<String, String> params;
    }
    

    It is possible to nest Maps, like Map<String, Map<String, String>> or Map<String, Object>, but you really should be using classes for this.

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

Sidebar

Related Questions

I want append to a string so that every time I loop over it,
I know I can loop over the string or build a regex or invert
I am trying to loop over a dictionary, specifically the request object's meta property.
I want to loop over the files that begin with '.' in directory x
I have the following command which will loop over all the subdirectories in a
I don't understand how to loop over a static dictionary contained in a static
A common 'Perlism' is generating a list as something to loop over in this
In a loop running over the entire string how do i peek at the
I'm trying to loop over distinct values over a dictionary list: So I have
I have a function, call it f , that takes a string and returns

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.