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
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
add a comment |
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
What did you try so far?
– Robert Kock
Mar 25 at 16:28
1
Did you meanchars instead ofbytes?
– 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 methodbuildValuesList()?
– 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
add a comment |
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
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
java
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 meanchars instead ofbytes?
– 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 methodbuildValuesList()?
– 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
add a comment |
What did you try so far?
– Robert Kock
Mar 25 at 16:28
1
Did you meanchars instead ofbytes?
– 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 methodbuildValuesList()?
– 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
add a comment |
1 Answer
1
active
oldest
votes
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;
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 thecurrentListwas reinitialized it was not added to thesplittedList
– Valentin Carnu
Mar 25 at 16:58
Thanks - that is exactly what i needed
– May
Mar 25 at 17:00
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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 thecurrentListwas reinitialized it was not added to thesplittedList
– Valentin Carnu
Mar 25 at 16:58
Thanks - that is exactly what i needed
– May
Mar 25 at 17:00
add a comment |
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;
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 thecurrentListwas reinitialized it was not added to thesplittedList
– Valentin Carnu
Mar 25 at 16:58
Thanks - that is exactly what i needed
– May
Mar 25 at 17:00
add a comment |
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;
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;
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 thecurrentListwas reinitialized it was not added to thesplittedList
– Valentin Carnu
Mar 25 at 16:58
Thanks - that is exactly what i needed
– May
Mar 25 at 17:00
add a comment |
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 thecurrentListwas reinitialized it was not added to thesplittedList
– 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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What did you try so far?
– Robert Kock
Mar 25 at 16:28
1
Did you mean
chars instead ofbytes?– 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