• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Subdividing a Collection into smaller collections

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the requirement I have to split a collection into n collections each of a given size. Here is the code, I came up with. I am guessing there is a better way to do this or is there a Collection Utility that does it for me. I looked but could not find it (did a google search and Java ranch search).
Any pointers would be great and thanks for your time.
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually found a sublist after I posted the message. Is that the better way of doing this.
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class CollectionSplitter {

static List split(List srcList, int index) {
List d = new ArrayList();
for (int i = 0; i < srcList.size(); i = i + 4) {
if ((i + 4) <= srcList.size())
d.add(srcList.subList(i, i + 4));
else
d.add(srcList.subList(i, srcList.size() - 1));
}
return d;
}

public static void main(String[] args) {
List resultList = new ArrayList();
List list = new ArrayList();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
list.add("six");
list.add("seven");
list.add("eight");
list.add("nine");
list.add("ten");
list.add("eleven");
resultList = split(list, 5);
}
}

if there is still simple let me know
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gowtham,
Yours looks simple. Anyway here is my new version

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about something like

 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realise this an old post but it comes up high in web searches for this topic.

So, here is an alternative solution basically adapted from the google collections apis. It uses generics to return a list of lists which are of the specified size. Just call the static method partition() with your parameters.


 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

thanks this discussion was helpful. Based on your discussion I have created a little summary and a JUnit test which can be found here:
http://www.vogella.de/articles/JavaAlgorithmsPartitionCollection/article.html

I did include this discussion as the source of information in the article.

Thanks again, Lars
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niran Abeygunawardena wrote:I realise this an old post but it comes up high in web searches for this topic.

So, There is another alternative solution by using enumerations and not instantiating multiple list objects at once .


 
This tiny ad will self destruct in five seconds.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic