Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

List split problem

 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have list of String objects defined as List<String> and I want to split this List into sublist such that each sublist should be of length <=6 and number of chars in each sublist should not exceed 240 chars.

Any idea how to achieve this in java 8 ?

Special condition : If the total no. of chars in the sublist exceeds 240 chars then we have to decrease the no. of elements in the sublist such that it meets the length of 240 and shift the rest elements in next sublist and so on.

Regards,
Khuzema
 
Sheriff
Posts: 7602
521
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what have you got so far.

My recommendation would be to reduce those numbers in the first sentence so to come up with the solution would be considerably easier. From the requirement (first sentence) I'm assuming there might be N levels of nesting, correct?
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Khuzema Dharwala wrote:If the total no. of chars in the sublist exceeds 240 chars then we have to decrease the no. of elements in the sublist such that it meets the length of 240 and shift the rest elements in next sublist and so on.



Seems like you already have a particular implementation in mind? I wouldn't do it that way. I would go through the list and consider each String. If it's going to make the existing sublist longer than 6 or bigger in total than 240 characters, I would create a new empty sublist before proceeding with that String.

(That would not work correctly if one of the Strings in the original list was longer than 240 characters, it might even cause an infinite loop depending on the implementation. But that's all right because the requirements don't say what should happen in that case.)
 
Rancher
Posts: 157
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After writing this, I realize I may have misinterpreted it. My interpretation was that each of the split Lists need to be of the same size (the size <= 6). If that's not true, then not all of my answer will apply.

My approach here would be to first discover the sublist length before you begin splitting it into sublists. You really don't want to get halfway through and then find out your sublists need to be shorter than you originally thought.

Here's what I'd do. First write a method to calculate the total characters from index a to b of a List of Strings:


After that, write a method to calculate the sublist size required. You'll probably need a nested for loop of some kind, basically you're going to try starting with 6, see if the rules are violated. If not, you return 6. If so, you decrease by 1 and try again.

After you find the sublist size, write a method to break a list into multiple lists.

Your approach here will probably be to use another for loop going to the sublist size. Each iteration of the for loop will create a new List<String> and add it to the List<List<String>>. At the end, each inner list of string will be of the desired size.
 
Liutauras Vilda
Sheriff
Posts: 7602
521
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Ok, re-read once again. Yesterday I think I slightly misunderstood the text.

So really what you need to do is, let's assume a starting list contains a singular element of 480 chars, so you need to divide it in chunks of 40 chars (12 in total) and create two sublists with 6 chunks in each, right?

So my question is, what you can and what you can't do when a starting list contains 2 or more elements. Do those elements can be combined? Asking this, because that depends whether you efficiently would use those element spaces in each sublist.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I count three different interpretations of the question. (You may have heard the story about the seven blind men and the elephant.) Therefore I think it would help if we got a more complete description of the requirements. Khuzema? More info? Or have you found the right answer while the posters here are fumbling around?
 
What's a year in metric? Do you know this metric stuff tiny ad?
Learn to write code others love to read, one example at a time!
java.by-comparison.com
    Bookmark Topic Watch Topic
  • New Topic