@Test public void testSplit() { try { SetUtils.split(null, 0); Assert.fail("Expected IllegalArgumentException."); } catch (IllegalArgumentException e) { } int size = 10; List<Set<Integer>> subsets = SetUtils.split(null, size); Assert.assertEquals(0, subsets.size()); subsets = SetUtils.split(Collections.<Integer>emptySet(), size); Assert.assertEquals(0, subsets.size()); subsets = SetUtils.split(Collections.singleton(0), size); Assert.assertEquals(1, subsets.size()); Assert.assertEquals(1, subsets.get(0).size()); Set<Integer> set = new LinkedHashSet<Integer>(5); for(int i = 0; i < 5; i++) { set.add(i); } subsets = SetUtils.split(set, size); Assert.assertEquals(1, subsets.size()); Assert.assertEquals(5, subsets.get(0).size()); set = new LinkedHashSet<Integer>(10); for(int i = 0; i < 10; i++) { set.add(i); } subsets = SetUtils.split(set, size); Assert.assertEquals(1, subsets.size()); Assert.assertEquals(10, subsets.get(0).size()); set = new LinkedHashSet<Integer>(11); for(int i = 0; i < 11; i++) { set.add(i); } subsets = SetUtils.split(set, size); Assert.assertEquals(2, subsets.size()); Assert.assertEquals(10, subsets.get(0).size()); Assert.assertEquals(1, subsets.get(1).size()); set = new LinkedHashSet<Integer>(20); for(int i = 0; i < 20; i++) { set.add(i); } subsets = SetUtils.split(set, size); Assert.assertEquals(2, subsets.size()); Assert.assertEquals(10, subsets.get(0).size()); Assert.assertEquals(10, subsets.get(1).size()); set = new LinkedHashSet<Integer>(27); for(int i = 0; i < 27; i++) { set.add(i); } subsets = SetUtils.split(set, size); Assert.assertEquals(3, subsets.size()); Assert.assertEquals(10, subsets.get(0).size()); Assert.assertEquals(10, subsets.get(1).size()); Assert.assertEquals(7, subsets.get(2).size()); }
public static <T> List<Set<T>> split(Set<T> original, int subsetSize) { if(subsetSize <= 0) { throw new IllegalArgumentException("Incorrect max size"); } if(original == null || original.isEmpty()) { return Collections.emptyList(); } int subsetCount = (int) (Math.ceil((double)original.size() / subsetSize)); ArrayList<Set<T>> subsets = new ArrayList<Set<T>>(subsetCount); Iterator<T> iterator = original.iterator(); for(int i = 0; i < subsetCount; i++) { Set<T> subset = new LinkedHashSet<T>(subsetSize); for(int j = 0; j < subsetSize && iterator.hasNext(); j++) { subset.add(iterator.next()); } subsets.add(subset); } return subsets; }
SetUtils { public static <T> List<Set<T>> split(Set<T> original, int subsetSize) { if(subsetSize <= 0) { throw new IllegalArgumentException("Incorrect max size"); } if(original == null || original.isEmpty()) { return Collections.emptyList(); } int subsetCount = (int) (Math.ceil((double)original.size() / subsetSize)); ArrayList<Set<T>> subsets = new ArrayList<Set<T>>(subsetCount); Iterator<T> iterator = original.iterator(); for(int i = 0; i < subsetCount; i++) { Set<T> subset = new LinkedHashSet<T>(subsetSize); for(int j = 0; j < subsetSize && iterator.hasNext(); j++) { subset.add(iterator.next()); } subsets.add(subset); } return subsets; } }
SetUtils { public static <T> List<Set<T>> split(Set<T> original, int subsetSize) { if(subsetSize <= 0) { throw new IllegalArgumentException("Incorrect max size"); } if(original == null || original.isEmpty()) { return Collections.emptyList(); } int subsetCount = (int) (Math.ceil((double)original.size() / subsetSize)); ArrayList<Set<T>> subsets = new ArrayList<Set<T>>(subsetCount); Iterator<T> iterator = original.iterator(); for(int i = 0; i < subsetCount; i++) { Set<T> subset = new LinkedHashSet<T>(subsetSize); for(int j = 0; j < subsetSize && iterator.hasNext(); j++) { subset.add(iterator.next()); } subsets.add(subset); } return subsets; }  }
SetUtils { public static <T> List<Set<T>> split(Set<T> original, int subsetSize) { if(subsetSize <= 0) { throw new IllegalArgumentException("Incorrect max size"); } if(original == null || original.isEmpty()) { return Collections.emptyList(); } int subsetCount = (int) (Math.ceil((double)original.size() / subsetSize)); ArrayList<Set<T>> subsets = new ArrayList<Set<T>>(subsetCount); Iterator<T> iterator = original.iterator(); for(int i = 0; i < subsetCount; i++) { Set<T> subset = new LinkedHashSet<T>(subsetSize); for(int j = 0; j < subsetSize && iterator.hasNext(); j++) { subset.add(iterator.next()); } subsets.add(subset); } return subsets; }  static List<Set<T>> split(Set<T> original, int subsetSize); }
SetUtils { public static <T> List<Set<T>> split(Set<T> original, int subsetSize) { if(subsetSize <= 0) { throw new IllegalArgumentException("Incorrect max size"); } if(original == null || original.isEmpty()) { return Collections.emptyList(); } int subsetCount = (int) (Math.ceil((double)original.size() / subsetSize)); ArrayList<Set<T>> subsets = new ArrayList<Set<T>>(subsetCount); Iterator<T> iterator = original.iterator(); for(int i = 0; i < subsetCount; i++) { Set<T> subset = new LinkedHashSet<T>(subsetSize); for(int j = 0; j < subsetSize && iterator.hasNext(); j++) { subset.add(iterator.next()); } subsets.add(subset); } return subsets; }  static List<Set<T>> split(Set<T> original, int subsetSize);  }