ramananravi commented on pull request #269: URL: https://github.com/apache/commons-collections/pull/269#issuecomment-983961799
> There are a lot of shorter ways to do this using Java 8 features: https://stackoverflow.com/questions/30995763/java-8-partition-list @garydgregory Thanks a lot for the suggestion. One thing to be noticed about the current implementation is we are iterating the input collection just once before we derive the return list. On the other hand, if we use java 8 stream-collect-groupingBy pipeline, we get a Map<K, List> and we will have to iterate this List (values()) over once again to map it back to the original input collection type. Sorry if am missing something obvious here, but I shall paste the code here for reference, please let me know if there are better suggestions. Thanks in advance. List<T> returnList = new ArrayList<>(); final AtomicInteger counter = new AtomicInteger(0); final Map<Integer, T> map = (Map<Integer, T>) collection.stream() .collect(Collectors.groupingBy(i -> counter.getAndIncrement() / chunkSize)); for (T t : map.values()) { T tempCollection = (T) collection.getClass().newInstance(); Iterator iterator = t.iterator(); while (iterator.hasNext()) { tempCollection.add(iterator.next()); } returnList.add(tempCollection); } return returnList; -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
