@Test
public void testToMapWithFactory() {

    Observable<String> source = Observable.from("a", "bb", "ccc", "dddd");
    Func0<Map<Integer, String>> mapFactory = new Func0<Map<Integer, String>>() {
        @Override
        public Map<Integer, String> call() {
            return new LinkedHashMap<Integer, String>() {
                @Override
                protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
                    return size() > 3;
                }
            };
        }
    };
    Func1<String, Integer> lengthFunc = new Func1<String, Integer>() {
        @Override
        public Integer call(String t1) {
            return t1.length();
        }
    };
    Observable<Map<Integer, String>> mapped = Observable.create(OperationToMap.toMap(source, lengthFunc, Functions.<String> identity(), mapFactory));
    Map<Integer, String> expected = new LinkedHashMap<Integer, String>();
    expected.put(2, "bb");
    expected.put(3, "ccc");
    expected.put(4, "dddd");
    mapped.subscribe(objectObserver);
    verify(objectObserver, never()).onError(any(Throwable.class));
    verify(objectObserver, times(1)).onNext(expected);
    verify(objectObserver, times(1)).onCompleted();
}