java - Efficient way to divide a list into lists of n size -
i have array, want divide smaller arrays of n size, , perform operation on each. current method of doing is
implemented arraylists in java (any pseudocode do)
(int = 1; <= math.floor((a.size() / n)); i++) { arraylist temp = subarray(a, ((i * n) - n), (i * n) - 1); // stuff temp } private arraylist<comparable> subarray(arraylist a, int start, int end) { arraylist toreturn = new arraylist(); (int = start; <= end; i++) { toreturn.add(a.get(i)); } return toreturn; }
where list, n size of desired lists
i believe way taking time when working considerably large lists (of 1 million in size) i'm trying figure out more efficient.
you'll want makes use of list.sublist(int, int) views rather copying each sublist. easily, use guava's lists.partition(list, int) method:
list<foo> foos = ... (list<foo> partition : lists.partition(foos, n)) { // partition }
note this, many things, isn't efficient list
isn't randomaccess
(such linkedlist
).
I just wanted to let you know that the piece you shared with us is pretty unique and authentic. I am going to keep browsing this blog.
ReplyDeleteAndroid Training Institute in Chennai | Android Training Institute in anna nagar | Android Training Institute in omr | Android Training Institute in porur | Android Training Institute in tambaram | Android Training Institute in velachery