Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Subdividing a Collection into smaller collections

 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I actually found a sublist after I posted the message. Is that the better way of doing this.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Gowtham,
Yours looks simple. Anyway here is my new version

 
Vihung Marathe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about something like

 
Niran Abeygunawardena
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.


 
Lars Vogel
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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
 
Mehmet Ali Kipcak
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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 .


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic