public <K> FlowToMap<K, List<B>, FlowGroup<K, B>> toMap(Supplier<Map<K, FlowGroup<K, B>>> groupMapSupplier, Mapper<B, K> toKey) { return toMap(groupMapSupplier, toKey, Flow.<K, B>flowGroupListMapper()); }
public <K, V> FlowToMap<K, V, FlowGroup<K, B>> toMap(Mapper<B, K> toKey, Mapper<FlowGroup<K, B>, V> toValue) { return toMap(Flow.<K, FlowGroup<K, B>>hashMapSupplier(), toKey, toValue); }
public <K, V> FlowToMap<K, V, FlowGroup<K, B>> toMap(Mapper<B, K> toKey, Mapper<FlowGroup<K, B>, V> toValue) { return toMap(Flow.<K, FlowGroup<K, B>>hashMapSupplier(), toKey, toValue); }
public <K> FlowToMap<K, List<B>, FlowGroup<K, B>> toMap(Supplier<Map<K, FlowGroup<K, B>>> groupMapSupplier, Mapper<B, K> toKey) { return toMap(groupMapSupplier, toKey, Flow.<K, B>flowGroupListMapper()); }
@Test public void test_toMap() { Map<Boolean, List<Integer>> map = Flow.of(1, 2, 3) .toMap(new Mapper<Integer, Boolean>() { @Override public Boolean map(Integer integer) { return integer % 2 == 0; } }, Flow.<Boolean, Integer>flowGroupListMapper()) .get(); Assert.assertEquals(2, map.size()); Assert.assertEquals(1, map.get(true).size()); Assert.assertEquals(Integer.valueOf(2), map.get(true).get(0)); Assert.assertEquals(2, map.get(false).size()); Assert.assertEquals(Integer.valueOf(1), map.get(false).get(0)); Assert.assertEquals(Integer.valueOf(3), map.get(false).get(1)); }
@Test public void test_toMap_6() { Map<Boolean, List<Integer>> map = Flow.of(1, 2, 3) .toMap(new Supplier<Map<Boolean, FlowGroup<Boolean, Integer>>>() { @Override public Map<Boolean, FlowGroup<Boolean, Integer>> supply() { return new TreeMap<Boolean, FlowGroup<Boolean, Integer>>(); } }, new Mapper<Integer, Boolean>() { @Override public Boolean map(Integer integer) { return integer % 2 == 0; } }) .get(); Assert.assertEquals(2, map.size()); Assert.assertEquals(1, map.get(true).size()); Assert.assertEquals(Integer.valueOf(2), map.get(true).get(0)); Assert.assertEquals(2, map.get(false).size()); Assert.assertEquals(Integer.valueOf(1), map.get(false).get(0)); Assert.assertEquals(Integer.valueOf(3), map.get(false).get(1)); }
@Test public void test_toMap_4() { Map<Boolean, List<String>> map = Flow.of(1, 2, 3) .toMap(new Mapper<Integer, Boolean>() { @Override public Boolean map(Integer integer) { return integer % 2 == 0; } }, new Mapper<FlowGroup<Boolean, Integer>, List<String>>() { @Override public List<String> map(FlowGroup<Boolean, Integer> group) { return group.map(new Mapper<Integer, String>() { @Override public String map(Integer integer) { return integer.toString(); } }).toList().get(); } }) .get(); Assert.assertEquals(2, map.size()); Assert.assertEquals(1, map.get(true).size()); Assert.assertEquals("2", map.get(true).get(0)); Assert.assertEquals(2, map.get(false).size()); Assert.assertEquals("1", map.get(false).get(0)); Assert.assertEquals("3", map.get(false).get(1)); }