Jdk8 cut list collection by size

User 3082688 2022-06-24 05:48:57 阅读数:45

jdk8jdkcutlistcollection

Cut to size List, And back to list aggregate , Pictured :

  • Test chart
    image.png
    @Test public void testOne(){ List<String> testList =new ArrayList<>(); testList.add("a"); testList.add("b"); testList.add("c"); testList.add("d"); testList.add("e"); System.out.println(" According to each group 2 For cutting :"+splitList(testList,2)); System.out.println(" According to each group 3 For cutting :"+splitList(testList, 3)); System.out.println(" According to each group 1000 For cutting :"+splitList(testList, 1000)); } public static <T> List<List<T>> splitList(List<T> list,Integer number){ List<List<T>> splitList=new ArrayList<>(); if(list.size()<=number){ splitList.add(list); } else { Integer limit = (list.size() + number - 1) / number; splitList = Stream.iterate(0, n -> n + 1).limit(limit).parallel().map(a -> list.stream().skip(a * number).limit(number).parallel().collect (Collectors.toList())).collect(Collectors.toList()); } return splitList; }
  • Core code
copyright:author[User 3082688],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/20210803192914961o.html