NonNegativeIntegerSplitterByWeights { public static <R, C> ImmutableTable<R, C, Integer> splitByTableWeights( ImmutableTable<R, C, Double> weights, int numberToSplit) { ImmutableMap<Cell<R, C, Double>, Double> weightMap = weights.cellSet().stream() .collect(ImmutableMap.toImmutableMap( Function.identity(), Cell::getValue)); ImmutableMap<Cell<R, C, Double>, Integer> split = new NonNegativeIntegerSplitterByWeights<>(weightMap).split(numberToSplit); return split.keySet().stream() .collect(ImmutableTable.toImmutableTable( Cell::getRowKey, Cell::getColumnKey, split::get)); } NonNegativeIntegerSplitterByWeights(ImmutableMap<E, Double> weights); static NonNegativeIntegerSplitterByWeights<E> createWithSorting(
            ImmutableMap<E, Double> weights); static NonNegativeIntegerSplitterByWeights<E> createFromInts(ImmutableMap<E, Integer> weights); static NonNegativeIntegerSplitterByWeights<E> createFromIntsWithSorting(
            ImmutableMap<E, Integer> weights); static ImmutableTable<R, C, Integer> splitByTableWeights(
            ImmutableTable<R, C, Double> weights,
            int numberToSplit); static ImmutableTable<R, C, Integer> splitByIntegerTableWeights(
            ImmutableTable<R, C, Integer> weights,
            int numberToSplit); ImmutableMap<E, Integer> split(int numberToSplit);  }
@Test void testTableSplitting() { ImmutableTable<String, String, Double> tableWeights = ImmutableTableFactory.createWithRows( ImmutableMap.of( "Column1", 1d, "Column2", 2d), ImmutableList.of("Row1", "Row2")); ImmutableTable<String, String, Integer> splitInts = NonNegativeIntegerSplitterByWeights.splitByTableWeights(tableWeights, 2); Assertions.assertEquals(0, (int) splitInts.get("Row1", "Column1")); Assertions.assertEquals(1, (int) splitInts.get("Row1", "Column2")); Assertions.assertEquals(0, (int) splitInts.get("Row2", "Column1")); Assertions.assertEquals(1, (int) splitInts.get("Row2", "Column2")); }