How to dynamically create a list of lists based on the byte size of the elements in a listHow do I create a Java string from the contents of a file?How to create a generic array in Java?How can I create an executable JAR with dependencies using Maven?How to make a new List in JavaHow do I create a file and write to it in Java?How to convert byte size into human readable format in java?Convert Set to List without creating new ListHow to convert a byte array to a hex string in Java?How to convert Java String into byte[]?Using Java need to create a hashmap that is populated with data from a file

German idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)

Does it make sense to (partially) create a conlang that you don't intend to actually use in the story?

I agreed to cancel a long-planned vacation (with travel costs) due to project deadlines, but now the timeline has all changed again

Apollo Mission Operations Control Room 2 display, what do these numbers indicate?

How does mmorpg store data?

How useful would a hydroelectric power plant be in the post-apocalypse world?

What are the children of two Muggle-borns called?

Why would Dementors torture a Death Eater if they are loyal to Voldemort?

Tricolour nonogram

Does Flashpoint ever indicate directly that it takes place in Toronto?

Chandra exiles a card, I play it, it gets exiled again

Do electrons really perform instantaneous quantum leaps?

Why are symbols not written in words?

Does "boire un jus" tend to mean "coffee" or "juice of fruit"?

A 2nd hand guitar pitch sounds weird

Word ending in "-ine" for rat-like

Could you fall off a planet if it was being accelerated by engines?

What could a Medieval society do with excess animal blood?

Position representation of spin states and spin operators

Why was Pan Am Flight 103 flying over Lockerbie?

Customs and immigration on a USA-UK-Sweden flight itinerary

Why doesn't this nested command substitution of "ps" produce the expected output?

"I am [the / an] owner of a bookstore"?

Any Tips On Writing Extended Recollection In A Novel



How to dynamically create a list of lists based on the byte size of the elements in a list


How do I create a Java string from the contents of a file?How to create a generic array in Java?How can I create an executable JAR with dependencies using Maven?How to make a new List in JavaHow do I create a file and write to it in Java?How to convert byte size into human readable format in java?Convert Set to List without creating new ListHow to convert a byte array to a hex string in Java?How to convert Java String into byte[]?Using Java need to create a hashmap that is populated with data from a file













1















How can we create a list of lists based on the size in bytes of the elements in the list?



Given I have List values. I want to split this list into multiple lists based on the sum of the byte size of the elements.



List<List<String>> splitList(List<String> listToSplit, int maxByteSize)


Assuming I have a ListlistToSplit = Arrays.asList("Value1", "Value2", "Value3"));



And the maxByteSize = 12.



Then I would expect to get 2 Lists - one list containing Value1 and Value2 and the other Value3



This is what I have tried so far



 private List<List<String>> splitList( List<String> values, int maxByteSize) 
List<List<String>> returnList = new ArrayList<>();
returnList = buildValuesList(maxByteSize, values.size(), values, returnList);
return returnList;


private List<List<String>> buildList(int availableSize, int noOfValues, List<String> values,
List<List<String>> returnList)
List<String> valuesList = new ArrayList<>();
List<String> remainderValues = new ArrayList<>(values);
returnList.add(valuesList);
int currentSize = 0;
for (String val : values)
int valueSize = val.getBytes().length;
if (currentSize < availableSize && (currentSize + valueSize < availableSize))
currentSize += valueSize;
valuesList.add(val);
remainderValues.remove(val);
else
buildValuesList(availableSize, noOfValues, remainderValues, returnList);


return returnList;










share|improve this question
























  • What did you try so far?

    – Robert Kock
    Mar 25 at 16:28






  • 1





    Did you mean chars instead of bytes?

    – NeplatnyUdaj
    Mar 25 at 16:32











  • Amended question with what i have tried. I need the byte size. So can do String.getBytes().length. ATM i am not considering the charset

    – May
    Mar 25 at 16:36











  • What's your method buildValuesList()?

    – Robert Kock
    Mar 25 at 16:37











  • I've amended my post and added the splitList method which invokes the buildValuesList.

    – May
    Mar 25 at 16:43















1















How can we create a list of lists based on the size in bytes of the elements in the list?



Given I have List values. I want to split this list into multiple lists based on the sum of the byte size of the elements.



List<List<String>> splitList(List<String> listToSplit, int maxByteSize)


Assuming I have a ListlistToSplit = Arrays.asList("Value1", "Value2", "Value3"));



And the maxByteSize = 12.



Then I would expect to get 2 Lists - one list containing Value1 and Value2 and the other Value3



This is what I have tried so far



 private List<List<String>> splitList( List<String> values, int maxByteSize) 
List<List<String>> returnList = new ArrayList<>();
returnList = buildValuesList(maxByteSize, values.size(), values, returnList);
return returnList;


private List<List<String>> buildList(int availableSize, int noOfValues, List<String> values,
List<List<String>> returnList)
List<String> valuesList = new ArrayList<>();
List<String> remainderValues = new ArrayList<>(values);
returnList.add(valuesList);
int currentSize = 0;
for (String val : values)
int valueSize = val.getBytes().length;
if (currentSize < availableSize && (currentSize + valueSize < availableSize))
currentSize += valueSize;
valuesList.add(val);
remainderValues.remove(val);
else
buildValuesList(availableSize, noOfValues, remainderValues, returnList);


return returnList;










share|improve this question
























  • What did you try so far?

    – Robert Kock
    Mar 25 at 16:28






  • 1





    Did you mean chars instead of bytes?

    – NeplatnyUdaj
    Mar 25 at 16:32











  • Amended question with what i have tried. I need the byte size. So can do String.getBytes().length. ATM i am not considering the charset

    – May
    Mar 25 at 16:36











  • What's your method buildValuesList()?

    – Robert Kock
    Mar 25 at 16:37











  • I've amended my post and added the splitList method which invokes the buildValuesList.

    – May
    Mar 25 at 16:43













1












1








1








How can we create a list of lists based on the size in bytes of the elements in the list?



Given I have List values. I want to split this list into multiple lists based on the sum of the byte size of the elements.



List<List<String>> splitList(List<String> listToSplit, int maxByteSize)


Assuming I have a ListlistToSplit = Arrays.asList("Value1", "Value2", "Value3"));



And the maxByteSize = 12.



Then I would expect to get 2 Lists - one list containing Value1 and Value2 and the other Value3



This is what I have tried so far



 private List<List<String>> splitList( List<String> values, int maxByteSize) 
List<List<String>> returnList = new ArrayList<>();
returnList = buildValuesList(maxByteSize, values.size(), values, returnList);
return returnList;


private List<List<String>> buildList(int availableSize, int noOfValues, List<String> values,
List<List<String>> returnList)
List<String> valuesList = new ArrayList<>();
List<String> remainderValues = new ArrayList<>(values);
returnList.add(valuesList);
int currentSize = 0;
for (String val : values)
int valueSize = val.getBytes().length;
if (currentSize < availableSize && (currentSize + valueSize < availableSize))
currentSize += valueSize;
valuesList.add(val);
remainderValues.remove(val);
else
buildValuesList(availableSize, noOfValues, remainderValues, returnList);


return returnList;










share|improve this question
















How can we create a list of lists based on the size in bytes of the elements in the list?



Given I have List values. I want to split this list into multiple lists based on the sum of the byte size of the elements.



List<List<String>> splitList(List<String> listToSplit, int maxByteSize)


Assuming I have a ListlistToSplit = Arrays.asList("Value1", "Value2", "Value3"));



And the maxByteSize = 12.



Then I would expect to get 2 Lists - one list containing Value1 and Value2 and the other Value3



This is what I have tried so far



 private List<List<String>> splitList( List<String> values, int maxByteSize) 
List<List<String>> returnList = new ArrayList<>();
returnList = buildValuesList(maxByteSize, values.size(), values, returnList);
return returnList;


private List<List<String>> buildList(int availableSize, int noOfValues, List<String> values,
List<List<String>> returnList)
List<String> valuesList = new ArrayList<>();
List<String> remainderValues = new ArrayList<>(values);
returnList.add(valuesList);
int currentSize = 0;
for (String val : values)
int valueSize = val.getBytes().length;
if (currentSize < availableSize && (currentSize + valueSize < availableSize))
currentSize += valueSize;
valuesList.add(val);
remainderValues.remove(val);
else
buildValuesList(availableSize, noOfValues, remainderValues, returnList);


return returnList;







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 16:42







May

















asked Mar 25 at 16:26









MayMay

62 bronze badges




62 bronze badges












  • What did you try so far?

    – Robert Kock
    Mar 25 at 16:28






  • 1





    Did you mean chars instead of bytes?

    – NeplatnyUdaj
    Mar 25 at 16:32











  • Amended question with what i have tried. I need the byte size. So can do String.getBytes().length. ATM i am not considering the charset

    – May
    Mar 25 at 16:36











  • What's your method buildValuesList()?

    – Robert Kock
    Mar 25 at 16:37











  • I've amended my post and added the splitList method which invokes the buildValuesList.

    – May
    Mar 25 at 16:43

















  • What did you try so far?

    – Robert Kock
    Mar 25 at 16:28






  • 1





    Did you mean chars instead of bytes?

    – NeplatnyUdaj
    Mar 25 at 16:32











  • Amended question with what i have tried. I need the byte size. So can do String.getBytes().length. ATM i am not considering the charset

    – May
    Mar 25 at 16:36











  • What's your method buildValuesList()?

    – Robert Kock
    Mar 25 at 16:37











  • I've amended my post and added the splitList method which invokes the buildValuesList.

    – May
    Mar 25 at 16:43
















What did you try so far?

– Robert Kock
Mar 25 at 16:28





What did you try so far?

– Robert Kock
Mar 25 at 16:28




1




1





Did you mean chars instead of bytes?

– NeplatnyUdaj
Mar 25 at 16:32





Did you mean chars instead of bytes?

– NeplatnyUdaj
Mar 25 at 16:32













Amended question with what i have tried. I need the byte size. So can do String.getBytes().length. ATM i am not considering the charset

– May
Mar 25 at 16:36





Amended question with what i have tried. I need the byte size. So can do String.getBytes().length. ATM i am not considering the charset

– May
Mar 25 at 16:36













What's your method buildValuesList()?

– Robert Kock
Mar 25 at 16:37





What's your method buildValuesList()?

– Robert Kock
Mar 25 at 16:37













I've amended my post and added the splitList method which invokes the buildValuesList.

– May
Mar 25 at 16:43





I've amended my post and added the splitList method which invokes the buildValuesList.

– May
Mar 25 at 16:43










1 Answer
1






active

oldest

votes


















0














Iterate over the listToSplit and if the sum between the bytes length of the current element and the bytes length of previous elements is greater than the maxByteSize then add the current list to the list to return and reset the length counter of the strings added to the list to return



public List<List<String>> splitList(List<String> listToSplit, int maxByteSize) 
List<List<String>> splittedList = new ArrayList<>();
List<String> currentList = new ArrayList<>();
int currentSize = 0;
for (String in : listToSplit)

if (currentSize + in.getBytes().length > maxByteSize)
splittedList.add(currentList);
currentList = new ArrayList<>();
splittedList.add(currentList); // add the new list to the list to return
currentSize = 0;

currentSize += in.getBytes().length;
currentList.add(in);

return splittedList;






share|improve this answer

























  • That does not work. With this Unit test, we have only 1 list returned when it should be 2 e.g. public void testSplitList() String[] values = "value1", "value2", "value3", "value4", "value5"; List<List<String>> lists = splitList(Arrays.asList(values), 18); assertThat(lists.size(), equalTo(2));

    – May
    Mar 25 at 16:54












  • I've updated the answer, when the currentList was reinitialized it was not added to the splittedList

    – Valentin Carnu
    Mar 25 at 16:58











  • Thanks - that is exactly what i needed

    – May
    Mar 25 at 17:00










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%2f55342331%2fhow-to-dynamically-create-a-list-of-lists-based-on-the-byte-size-of-the-elements%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














Iterate over the listToSplit and if the sum between the bytes length of the current element and the bytes length of previous elements is greater than the maxByteSize then add the current list to the list to return and reset the length counter of the strings added to the list to return



public List<List<String>> splitList(List<String> listToSplit, int maxByteSize) 
List<List<String>> splittedList = new ArrayList<>();
List<String> currentList = new ArrayList<>();
int currentSize = 0;
for (String in : listToSplit)

if (currentSize + in.getBytes().length > maxByteSize)
splittedList.add(currentList);
currentList = new ArrayList<>();
splittedList.add(currentList); // add the new list to the list to return
currentSize = 0;

currentSize += in.getBytes().length;
currentList.add(in);

return splittedList;






share|improve this answer

























  • That does not work. With this Unit test, we have only 1 list returned when it should be 2 e.g. public void testSplitList() String[] values = "value1", "value2", "value3", "value4", "value5"; List<List<String>> lists = splitList(Arrays.asList(values), 18); assertThat(lists.size(), equalTo(2));

    – May
    Mar 25 at 16:54












  • I've updated the answer, when the currentList was reinitialized it was not added to the splittedList

    – Valentin Carnu
    Mar 25 at 16:58











  • Thanks - that is exactly what i needed

    – May
    Mar 25 at 17:00















0














Iterate over the listToSplit and if the sum between the bytes length of the current element and the bytes length of previous elements is greater than the maxByteSize then add the current list to the list to return and reset the length counter of the strings added to the list to return



public List<List<String>> splitList(List<String> listToSplit, int maxByteSize) 
List<List<String>> splittedList = new ArrayList<>();
List<String> currentList = new ArrayList<>();
int currentSize = 0;
for (String in : listToSplit)

if (currentSize + in.getBytes().length > maxByteSize)
splittedList.add(currentList);
currentList = new ArrayList<>();
splittedList.add(currentList); // add the new list to the list to return
currentSize = 0;

currentSize += in.getBytes().length;
currentList.add(in);

return splittedList;






share|improve this answer

























  • That does not work. With this Unit test, we have only 1 list returned when it should be 2 e.g. public void testSplitList() String[] values = "value1", "value2", "value3", "value4", "value5"; List<List<String>> lists = splitList(Arrays.asList(values), 18); assertThat(lists.size(), equalTo(2));

    – May
    Mar 25 at 16:54












  • I've updated the answer, when the currentList was reinitialized it was not added to the splittedList

    – Valentin Carnu
    Mar 25 at 16:58











  • Thanks - that is exactly what i needed

    – May
    Mar 25 at 17:00













0












0








0







Iterate over the listToSplit and if the sum between the bytes length of the current element and the bytes length of previous elements is greater than the maxByteSize then add the current list to the list to return and reset the length counter of the strings added to the list to return



public List<List<String>> splitList(List<String> listToSplit, int maxByteSize) 
List<List<String>> splittedList = new ArrayList<>();
List<String> currentList = new ArrayList<>();
int currentSize = 0;
for (String in : listToSplit)

if (currentSize + in.getBytes().length > maxByteSize)
splittedList.add(currentList);
currentList = new ArrayList<>();
splittedList.add(currentList); // add the new list to the list to return
currentSize = 0;

currentSize += in.getBytes().length;
currentList.add(in);

return splittedList;






share|improve this answer















Iterate over the listToSplit and if the sum between the bytes length of the current element and the bytes length of previous elements is greater than the maxByteSize then add the current list to the list to return and reset the length counter of the strings added to the list to return



public List<List<String>> splitList(List<String> listToSplit, int maxByteSize) 
List<List<String>> splittedList = new ArrayList<>();
List<String> currentList = new ArrayList<>();
int currentSize = 0;
for (String in : listToSplit)

if (currentSize + in.getBytes().length > maxByteSize)
splittedList.add(currentList);
currentList = new ArrayList<>();
splittedList.add(currentList); // add the new list to the list to return
currentSize = 0;

currentSize += in.getBytes().length;
currentList.add(in);

return splittedList;







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 17:00

























answered Mar 25 at 16:42









Valentin CarnuValentin Carnu

7281 gold badge3 silver badges15 bronze badges




7281 gold badge3 silver badges15 bronze badges












  • That does not work. With this Unit test, we have only 1 list returned when it should be 2 e.g. public void testSplitList() String[] values = "value1", "value2", "value3", "value4", "value5"; List<List<String>> lists = splitList(Arrays.asList(values), 18); assertThat(lists.size(), equalTo(2));

    – May
    Mar 25 at 16:54












  • I've updated the answer, when the currentList was reinitialized it was not added to the splittedList

    – Valentin Carnu
    Mar 25 at 16:58











  • Thanks - that is exactly what i needed

    – May
    Mar 25 at 17:00

















  • That does not work. With this Unit test, we have only 1 list returned when it should be 2 e.g. public void testSplitList() String[] values = "value1", "value2", "value3", "value4", "value5"; List<List<String>> lists = splitList(Arrays.asList(values), 18); assertThat(lists.size(), equalTo(2));

    – May
    Mar 25 at 16:54












  • I've updated the answer, when the currentList was reinitialized it was not added to the splittedList

    – Valentin Carnu
    Mar 25 at 16:58











  • Thanks - that is exactly what i needed

    – May
    Mar 25 at 17:00
















That does not work. With this Unit test, we have only 1 list returned when it should be 2 e.g. public void testSplitList() String[] values = "value1", "value2", "value3", "value4", "value5"; List<List<String>> lists = splitList(Arrays.asList(values), 18); assertThat(lists.size(), equalTo(2));

– May
Mar 25 at 16:54






That does not work. With this Unit test, we have only 1 list returned when it should be 2 e.g. public void testSplitList() String[] values = "value1", "value2", "value3", "value4", "value5"; List<List<String>> lists = splitList(Arrays.asList(values), 18); assertThat(lists.size(), equalTo(2));

– May
Mar 25 at 16:54














I've updated the answer, when the currentList was reinitialized it was not added to the splittedList

– Valentin Carnu
Mar 25 at 16:58





I've updated the answer, when the currentList was reinitialized it was not added to the splittedList

– Valentin Carnu
Mar 25 at 16:58













Thanks - that is exactly what i needed

– May
Mar 25 at 17:00





Thanks - that is exactly what i needed

– May
Mar 25 at 17:00






Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















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%2f55342331%2fhow-to-dynamically-create-a-list-of-lists-based-on-the-byte-size-of-the-elements%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 preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?