Java: Clone contents of stringbuilder and append item to each row all












-1















How can I clone contents of Stringbuilder based on number of records and append record to each row of cloned contents. I'm trying to write data to CSV in way each rows have duplicate columns values repeated by times size of records.



Sample Output CSV should look like.



CN1| CN2| CN3
1 b c
1 d f
2 g h


Here's sample Java Code I'm using



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVWrite {
public static Map<String, List<Records>> getData() {

Map<String, List<Records>> fields = new HashMap<>();

List<Records> records_1 = new ArrayList<>();

Records sample_1 = new Records();
sample_1.setName("b");
sample_1.setId("c");

Records sample_2 = new Records();
sample_2.setName("d");
sample_2.setId("f");

records_1.add(sample_1);
records_1.add(sample_2);

List<Records> records_2 = new ArrayList<>();

Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");

records_2.add(sample_3);

fields.put("1", records_1);
fields.put("2", records_2);

return fields;
}

public static void main(String args) {
CSVWrite write = new CSVWrite();
String csvString = write.getCsvString();
String expectedString = "1,b,cn" + "1,d,fn" + "2,g,h";
System.out.println(csvString.equals(expectedString));
}

public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
sb.append(entry.getKey());
List<Records> records = entry.getValue();

for (int i = 0; i < records.size(); i++) {
// how to clone contents of sb repeated times based on records size and append
// record items to all.
}
}
return sb.toString();
}
}









share|improve this question


















  • 1





    Unclear to me what you want to clone. The three expected output lines are different from each-other.

    – Thilo
    Nov 19 '18 at 9:08











  • Unrelated: read about java naming conventions. You only use _ in SOME_CONSTANT.

    – GhostCat
    Nov 19 '18 at 9:26






  • 1





    It is totally unclear to mean what exactly is the problem here. What do you want to clone? When do you want to clone it? In your example only the value 1 appears twice and I have no idea why.

    – Amongalen
    Nov 19 '18 at 9:28






  • 1





    I think you should explain to us your question, not in terms of what you think the implementation should be, as that is at XY problem. Instead, explain what the input is, and what the output is relative to that input.

    – RealSkeptic
    Nov 19 '18 at 10:00
















-1















How can I clone contents of Stringbuilder based on number of records and append record to each row of cloned contents. I'm trying to write data to CSV in way each rows have duplicate columns values repeated by times size of records.



Sample Output CSV should look like.



CN1| CN2| CN3
1 b c
1 d f
2 g h


Here's sample Java Code I'm using



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVWrite {
public static Map<String, List<Records>> getData() {

Map<String, List<Records>> fields = new HashMap<>();

List<Records> records_1 = new ArrayList<>();

Records sample_1 = new Records();
sample_1.setName("b");
sample_1.setId("c");

Records sample_2 = new Records();
sample_2.setName("d");
sample_2.setId("f");

records_1.add(sample_1);
records_1.add(sample_2);

List<Records> records_2 = new ArrayList<>();

Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");

records_2.add(sample_3);

fields.put("1", records_1);
fields.put("2", records_2);

return fields;
}

public static void main(String args) {
CSVWrite write = new CSVWrite();
String csvString = write.getCsvString();
String expectedString = "1,b,cn" + "1,d,fn" + "2,g,h";
System.out.println(csvString.equals(expectedString));
}

public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
sb.append(entry.getKey());
List<Records> records = entry.getValue();

for (int i = 0; i < records.size(); i++) {
// how to clone contents of sb repeated times based on records size and append
// record items to all.
}
}
return sb.toString();
}
}









share|improve this question


















  • 1





    Unclear to me what you want to clone. The three expected output lines are different from each-other.

    – Thilo
    Nov 19 '18 at 9:08











  • Unrelated: read about java naming conventions. You only use _ in SOME_CONSTANT.

    – GhostCat
    Nov 19 '18 at 9:26






  • 1





    It is totally unclear to mean what exactly is the problem here. What do you want to clone? When do you want to clone it? In your example only the value 1 appears twice and I have no idea why.

    – Amongalen
    Nov 19 '18 at 9:28






  • 1





    I think you should explain to us your question, not in terms of what you think the implementation should be, as that is at XY problem. Instead, explain what the input is, and what the output is relative to that input.

    – RealSkeptic
    Nov 19 '18 at 10:00














-1












-1








-1








How can I clone contents of Stringbuilder based on number of records and append record to each row of cloned contents. I'm trying to write data to CSV in way each rows have duplicate columns values repeated by times size of records.



Sample Output CSV should look like.



CN1| CN2| CN3
1 b c
1 d f
2 g h


Here's sample Java Code I'm using



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVWrite {
public static Map<String, List<Records>> getData() {

Map<String, List<Records>> fields = new HashMap<>();

List<Records> records_1 = new ArrayList<>();

Records sample_1 = new Records();
sample_1.setName("b");
sample_1.setId("c");

Records sample_2 = new Records();
sample_2.setName("d");
sample_2.setId("f");

records_1.add(sample_1);
records_1.add(sample_2);

List<Records> records_2 = new ArrayList<>();

Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");

records_2.add(sample_3);

fields.put("1", records_1);
fields.put("2", records_2);

return fields;
}

public static void main(String args) {
CSVWrite write = new CSVWrite();
String csvString = write.getCsvString();
String expectedString = "1,b,cn" + "1,d,fn" + "2,g,h";
System.out.println(csvString.equals(expectedString));
}

public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
sb.append(entry.getKey());
List<Records> records = entry.getValue();

for (int i = 0; i < records.size(); i++) {
// how to clone contents of sb repeated times based on records size and append
// record items to all.
}
}
return sb.toString();
}
}









share|improve this question














How can I clone contents of Stringbuilder based on number of records and append record to each row of cloned contents. I'm trying to write data to CSV in way each rows have duplicate columns values repeated by times size of records.



Sample Output CSV should look like.



CN1| CN2| CN3
1 b c
1 d f
2 g h


Here's sample Java Code I'm using



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVWrite {
public static Map<String, List<Records>> getData() {

Map<String, List<Records>> fields = new HashMap<>();

List<Records> records_1 = new ArrayList<>();

Records sample_1 = new Records();
sample_1.setName("b");
sample_1.setId("c");

Records sample_2 = new Records();
sample_2.setName("d");
sample_2.setId("f");

records_1.add(sample_1);
records_1.add(sample_2);

List<Records> records_2 = new ArrayList<>();

Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");

records_2.add(sample_3);

fields.put("1", records_1);
fields.put("2", records_2);

return fields;
}

public static void main(String args) {
CSVWrite write = new CSVWrite();
String csvString = write.getCsvString();
String expectedString = "1,b,cn" + "1,d,fn" + "2,g,h";
System.out.println(csvString.equals(expectedString));
}

public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
sb.append(entry.getKey());
List<Records> records = entry.getValue();

for (int i = 0; i < records.size(); i++) {
// how to clone contents of sb repeated times based on records size and append
// record items to all.
}
}
return sb.toString();
}
}






java csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 9:05









cryptickpcryptickp

5917




5917








  • 1





    Unclear to me what you want to clone. The three expected output lines are different from each-other.

    – Thilo
    Nov 19 '18 at 9:08











  • Unrelated: read about java naming conventions. You only use _ in SOME_CONSTANT.

    – GhostCat
    Nov 19 '18 at 9:26






  • 1





    It is totally unclear to mean what exactly is the problem here. What do you want to clone? When do you want to clone it? In your example only the value 1 appears twice and I have no idea why.

    – Amongalen
    Nov 19 '18 at 9:28






  • 1





    I think you should explain to us your question, not in terms of what you think the implementation should be, as that is at XY problem. Instead, explain what the input is, and what the output is relative to that input.

    – RealSkeptic
    Nov 19 '18 at 10:00














  • 1





    Unclear to me what you want to clone. The three expected output lines are different from each-other.

    – Thilo
    Nov 19 '18 at 9:08











  • Unrelated: read about java naming conventions. You only use _ in SOME_CONSTANT.

    – GhostCat
    Nov 19 '18 at 9:26






  • 1





    It is totally unclear to mean what exactly is the problem here. What do you want to clone? When do you want to clone it? In your example only the value 1 appears twice and I have no idea why.

    – Amongalen
    Nov 19 '18 at 9:28






  • 1





    I think you should explain to us your question, not in terms of what you think the implementation should be, as that is at XY problem. Instead, explain what the input is, and what the output is relative to that input.

    – RealSkeptic
    Nov 19 '18 at 10:00








1




1





Unclear to me what you want to clone. The three expected output lines are different from each-other.

– Thilo
Nov 19 '18 at 9:08





Unclear to me what you want to clone. The three expected output lines are different from each-other.

– Thilo
Nov 19 '18 at 9:08













Unrelated: read about java naming conventions. You only use _ in SOME_CONSTANT.

– GhostCat
Nov 19 '18 at 9:26





Unrelated: read about java naming conventions. You only use _ in SOME_CONSTANT.

– GhostCat
Nov 19 '18 at 9:26




1




1





It is totally unclear to mean what exactly is the problem here. What do you want to clone? When do you want to clone it? In your example only the value 1 appears twice and I have no idea why.

– Amongalen
Nov 19 '18 at 9:28





It is totally unclear to mean what exactly is the problem here. What do you want to clone? When do you want to clone it? In your example only the value 1 appears twice and I have no idea why.

– Amongalen
Nov 19 '18 at 9:28




1




1





I think you should explain to us your question, not in terms of what you think the implementation should be, as that is at XY problem. Instead, explain what the input is, and what the output is relative to that input.

– RealSkeptic
Nov 19 '18 at 10:00





I think you should explain to us your question, not in terms of what you think the implementation should be, as that is at XY problem. Instead, explain what the input is, and what the output is relative to that input.

– RealSkeptic
Nov 19 '18 at 10:00












1 Answer
1






active

oldest

votes


















0














First, there is an error I think, this code look weird. The var sample_3 is built but not correctly populated:



Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");


It should be:



Records sample_3 = new Records();
sample_3.setName("g");
sample_3.setId("h");


Finally, there is no need to clone anything to achieve the desired result, simply rewrite getCsvString() like this:



public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
List<Records> records = entry.getValue();

for (Records record : records) {
sb.append(entry.getKey());
sb.append(",").append(record.getName());
sb.append(",").append(record.getId());
sb.append("n");
}
}
return sb.toString();
}


Those 2 corrections will generate output:




1,b,c



1,d,f



2,g,h







share|improve this answer


























  • Note: in CSV, there is no space after the comma.

    – RealSkeptic
    Nov 19 '18 at 10:06











  • @RealSkeptic OK, fixed. Except that minor detail, does it answer your question ?

    – Benoit
    Nov 19 '18 at 10:10











  • @Benoit This does answers partly, let me update question clearly

    – cryptickp
    Nov 19 '18 at 10:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371314%2fjava-clone-contents-of-stringbuilder-and-append-item-to-each-row-all%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First, there is an error I think, this code look weird. The var sample_3 is built but not correctly populated:



Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");


It should be:



Records sample_3 = new Records();
sample_3.setName("g");
sample_3.setId("h");


Finally, there is no need to clone anything to achieve the desired result, simply rewrite getCsvString() like this:



public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
List<Records> records = entry.getValue();

for (Records record : records) {
sb.append(entry.getKey());
sb.append(",").append(record.getName());
sb.append(",").append(record.getId());
sb.append("n");
}
}
return sb.toString();
}


Those 2 corrections will generate output:




1,b,c



1,d,f



2,g,h







share|improve this answer


























  • Note: in CSV, there is no space after the comma.

    – RealSkeptic
    Nov 19 '18 at 10:06











  • @RealSkeptic OK, fixed. Except that minor detail, does it answer your question ?

    – Benoit
    Nov 19 '18 at 10:10











  • @Benoit This does answers partly, let me update question clearly

    – cryptickp
    Nov 19 '18 at 10:31
















0














First, there is an error I think, this code look weird. The var sample_3 is built but not correctly populated:



Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");


It should be:



Records sample_3 = new Records();
sample_3.setName("g");
sample_3.setId("h");


Finally, there is no need to clone anything to achieve the desired result, simply rewrite getCsvString() like this:



public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
List<Records> records = entry.getValue();

for (Records record : records) {
sb.append(entry.getKey());
sb.append(",").append(record.getName());
sb.append(",").append(record.getId());
sb.append("n");
}
}
return sb.toString();
}


Those 2 corrections will generate output:




1,b,c



1,d,f



2,g,h







share|improve this answer


























  • Note: in CSV, there is no space after the comma.

    – RealSkeptic
    Nov 19 '18 at 10:06











  • @RealSkeptic OK, fixed. Except that minor detail, does it answer your question ?

    – Benoit
    Nov 19 '18 at 10:10











  • @Benoit This does answers partly, let me update question clearly

    – cryptickp
    Nov 19 '18 at 10:31














0












0








0







First, there is an error I think, this code look weird. The var sample_3 is built but not correctly populated:



Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");


It should be:



Records sample_3 = new Records();
sample_3.setName("g");
sample_3.setId("h");


Finally, there is no need to clone anything to achieve the desired result, simply rewrite getCsvString() like this:



public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
List<Records> records = entry.getValue();

for (Records record : records) {
sb.append(entry.getKey());
sb.append(",").append(record.getName());
sb.append(",").append(record.getId());
sb.append("n");
}
}
return sb.toString();
}


Those 2 corrections will generate output:




1,b,c



1,d,f



2,g,h







share|improve this answer















First, there is an error I think, this code look weird. The var sample_3 is built but not correctly populated:



Records sample_3 = new Records();
sample_1.setName("g");
sample_1.setId("h");


It should be:



Records sample_3 = new Records();
sample_3.setName("g");
sample_3.setId("h");


Finally, there is no need to clone anything to achieve the desired result, simply rewrite getCsvString() like this:



public String getCsvString() {
Map<String, List<Records>> fields = CSVWrite.getData();
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<Records>> entry : fields.entrySet()) {
List<Records> records = entry.getValue();

for (Records record : records) {
sb.append(entry.getKey());
sb.append(",").append(record.getName());
sb.append(",").append(record.getId());
sb.append("n");
}
}
return sb.toString();
}


Those 2 corrections will generate output:




1,b,c



1,d,f



2,g,h








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 10:09

























answered Nov 19 '18 at 9:40









BenoitBenoit

2,2231826




2,2231826













  • Note: in CSV, there is no space after the comma.

    – RealSkeptic
    Nov 19 '18 at 10:06











  • @RealSkeptic OK, fixed. Except that minor detail, does it answer your question ?

    – Benoit
    Nov 19 '18 at 10:10











  • @Benoit This does answers partly, let me update question clearly

    – cryptickp
    Nov 19 '18 at 10:31



















  • Note: in CSV, there is no space after the comma.

    – RealSkeptic
    Nov 19 '18 at 10:06











  • @RealSkeptic OK, fixed. Except that minor detail, does it answer your question ?

    – Benoit
    Nov 19 '18 at 10:10











  • @Benoit This does answers partly, let me update question clearly

    – cryptickp
    Nov 19 '18 at 10:31

















Note: in CSV, there is no space after the comma.

– RealSkeptic
Nov 19 '18 at 10:06





Note: in CSV, there is no space after the comma.

– RealSkeptic
Nov 19 '18 at 10:06













@RealSkeptic OK, fixed. Except that minor detail, does it answer your question ?

– Benoit
Nov 19 '18 at 10:10





@RealSkeptic OK, fixed. Except that minor detail, does it answer your question ?

– Benoit
Nov 19 '18 at 10:10













@Benoit This does answers partly, let me update question clearly

– cryptickp
Nov 19 '18 at 10:31





@Benoit This does answers partly, let me update question clearly

– cryptickp
Nov 19 '18 at 10:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371314%2fjava-clone-contents-of-stringbuilder-and-append-item-to-each-row-all%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?