how to use string builder instead of string in my code

Hi Experts,

i want to display output as in string format

this the code :
how to do i use string builder instead of string.
or
is my code is the perfect code.i mean no perfomance issue will be caused?

if this not efficient way can any one suggest me alternative.
finally i need to get o/p as string only
public static void main(String args[]){
		List<Integer> lista = new ArrayList<>();		
		lista.add(123);
		lista.add(456);
		lista.add(758);
		lista.add(890);
		String value="[";
		for (Integer integer : lista) {
			value=value+"\""+integer+"\""+",";
			
		}
		value=value.substring(0,value.length()-1)+"]";
		System.out.println("value is"+value);
	}

o/p:["123","456","758","890"]

Open in new window


Thanks
LVL 2
srikoteshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You definitely need quotes around each number?
0
srikoteshAuthor Commented:
yes
0
CEHJCommented:
String s = lista.toString().replaceAll("(\\d+)", "\"$1\"");

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gurpsbassiCommented:
If yow want to use StringBuilder.....

public static void main(final String args[]) {
        final List<Integer> lista = new ArrayList<>();
        lista.add(123);
        lista.add(456);
        lista.add(758);
        lista.add(890);

        final StringBuilder builder = new StringBuilder();
        builder.append("[");
        final Iterator<Integer> it = lista.iterator();

        while (it.hasNext()) {
            builder.append("\"")
                   .append(it.next())
                   .append("\"");
            if (it.hasNext()) {
                builder.append(",");
            }
        }
        builder.append("]");
        System.out.println("value is" + builder.toString());
    }

Open in new window

0
srikoteshAuthor Commented:
hi cehj,

replaceAll("(\\d+)", "\"$1\"")

can u explain to me what this \\d and "\"$1 will play the role.
0
srikoteshAuthor Commented:
thank to both i am getting the expected result in two ways
0
srikoteshAuthor Commented:
hi experts,

i have one more scenario with map
Map<Integer,Integer> map = new HashMap<>();            
            map.put(123,456);
            map.put(124,457);
            map.put(125,458);

i want o/p like this :{"123":456, "124"=457, "125":458}
0
srikoteshAuthor Commented:
i want o/p like this :{"123":456, "124":457, "125":458}
0
gurpsbassiCommented:
public static void main(final String args[]) {
        final Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(123, 456);
        map.put(124, 457);
        map.put(125, 458);

        final StringBuilder builder = new StringBuilder();
        builder.append("{");
        final Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();

        while (it.hasNext()) {
            Map.Entry<Integer, Integer> entry = it.next();
            builder.append("\"")
            .append(entry.getKey())
            .append("\"")
            .append(":")
            .append(entry.getValue());
            if (it.hasNext()) {
                builder.append(",");
            }
        }
        builder.append("}");
        System.out.println("value is" + builder.toString());
    }

Open in new window

0
srikoteshAuthor Commented:
why stringbuilder and map taking as final variable
will it give better performance?
0
gurpsbassiCommented:
no. its just a matter of coding style.
My IDE is set to make local variables final.
0
CEHJCommented:
String s = map.toString().replaceAll("(\\d+)=", "\"$1\":");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
Excellent
0
CEHJCommented:
Of course, for a huge List, that won't be as efficient as StringBuilder, but i'm guessing your List isn't huge ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.