@Test
public void testToMultimapWithMapFactory() {

    Observable<String> source = Observable.from("a", "b", "cc", "dd", "eee", "fff");
    Func0<Map<Integer, Collection<String>>> mapFactory = new Func0<Map<Integer, Collection<String>>>() {
        @Override
        public Map<Integer, Collection<String>> call() {
            return new LinkedHashMap<Integer, Collection<String>>() {
                @Override
                protected boolean removeEldestEntry(Map.Entry<Integer, Collection<String>> eldest) {
                    return size() > 2;
                }
            };
        }
    };
    Observable<Map<Integer, Collection<String>>> mapped = Observable.create(
            OperationToMultimap.toMultimap(source,
                    lengthFunc, Functions.<String> identity(),
                    mapFactory, new DefaultMultimapCollectionFactory<Integer, String>()));
    Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
    expected.put(2, Arrays.asList("cc", "dd"));
    expected.put(3, Arrays.asList("eee", "fff"));
    mapped.subscribe(objectObserver);
    verify(objectObserver, never()).onError(any(Throwable.class));
    verify(objectObserver, times(1)).onNext(expected);
    verify(objectObserver, times(1)).onCompleted();
}