Created
April 3, 2013 13:59
-
-
Save tksmaru/5301447 to your computer and use it in GitHub Desktop.
Listを指定サイズ毎に分割するutility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class CollectionUtils { | |
/** | |
* Listを指定したサイズ毎に分割します。 | |
* | |
* @param origin 分割元のList | |
* @param size Listの分割単位 | |
* @return サイズ毎に分割されたList。但し、Listがnullまたは空の場合、もしくはsizeが0以下の場合は空のListを返す。 | |
*/ | |
public static <T> List<List<T>> devide(List<T> origin, int size) { | |
if (origin == null || origin.isEmpty() || size <= 0) { | |
return Collections.emptyList(); | |
} | |
int block = origin.size() / size + (origin.size() % size > 0 ? 1 : 0 ); | |
List<List<T>> devidedList = new ArrayList<List<T>>(block); | |
for (int i = 0; i < block; i ++) { | |
int start = i * size; | |
int end = Math.min(start + size, origin.size()); | |
devidedList.add(new ArrayList<T>(origin.subList(start, end))); | |
} | |
return devidedList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment