io.reactivex.Observable.map()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(235)

本文整理了Java中io.reactivex.Observable.map()方法的一些代码示例,展示了Observable.map()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.map()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:map

Observable.map介绍

[英]Returns an Observable that applies a specified function to each item emitted by the source ObservableSource and emits the results of these function applications.

Scheduler: map does not operate by default on a particular Scheduler.
[中]返回一个Observable,它将指定的函数应用于源ObservableSource发出的每个项,并发出这些函数应用程序的结果。
Scheduler:map默认情况下不会在特定的计划程序上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

@Override
public Observable<String> apply(Observable<Map<String, String>> o) {
  return o.map(new Function<Map<String, String>, String>() {
    @Override
    public String apply(Map<String, String> map) {
      return map.get("firstName");
    }
  });
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public Observable<Integer> apply(final Integer leftValue, Observable<Integer> rightValues) {
  return rightValues.map(new Function<Integer, Integer>() {
    @Override
    public Integer apply(Integer rightValue) throws Exception {
      return add.apply(leftValue, rightValue);
    }
  });
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public ObservableSource<Object> apply(Integer v) throws Exception {
    return Observable.range(1, 2).map(new Function<Integer, Object>() {
      @Override
      public Object apply(Integer w) throws Exception {
        throw new TestException();
      }
    });
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
  public ObservableSource<Object> apply(Observable<Object> o) throws Exception {
    return o.map(Functions.identity());
  }
});

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Object apply(Observable<Object> o) throws Exception {
    return o.map(Functions.identity());
  }
}, false, 1, 1, 1);

代码示例来源:origin: ReactiveX/RxJava

@Override
  public ObservableSource<Integer> apply(Integer v) throws Exception {
    return Observable.range(1, 2).map(new Function<Integer, Integer>() {
      @Override
      public Integer apply(Integer v) throws Exception {
        throw new TestException();
      }
    });
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
  public ObservableSource<Integer> apply(Integer v) throws Exception {
    return Observable.range(1, 2).map(new Function<Integer, Integer>() {
      @Override
      public Integer apply(Integer w) throws Exception {
        throw new TestException();
      }
    });
  }
}, true)

代码示例来源:origin: ReactiveX/RxJava

private Observable<Integer> mergeNAsyncStreamsOfN(final int outerSize, final int innerSize) {
  Observable<Observable<Integer>> os = Observable.range(1, outerSize)
  .map(new Function<Integer, Observable<Integer>>() {
    @Override
    public Observable<Integer> apply(Integer i) {
      return Observable.range(1, innerSize).subscribeOn(Schedulers.computation());
    }
  });
  return Observable.merge(os);
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public Observable<String> apply(final GroupedObservable<Integer, Integer> group) {
  return group.subscribeOn(Schedulers.newThread()).map(new Function<Integer, String>() {
    @Override
    public String apply(Integer t1) {
      System.out.println("Received: " + t1 + " on group : " + group.getKey());
      return "first groups: " + t1;
    }
  });
}

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = IllegalArgumentException.class)
public void testMapWithIssue417() {
  Observable.just(1).observeOn(Schedulers.computation())
      .map(new Function<Integer, Integer>() {
        @Override
        public Integer apply(Integer arg0) {
          throw new IllegalArgumentException("any error");
        }
      }).blockingSingle();
}

代码示例来源:origin: ReactiveX/RxJava

private Observable<Integer> mergeNSyncStreamsOfN(final int outerSize, final int innerSize) {
  Observable<Observable<Integer>> os = Observable.range(1, outerSize)
  .map(new Function<Integer, Observable<Integer>>() {
    @Override
    public Observable<Integer> apply(Integer i) {
      return Observable.range(1, innerSize);
    }
  });
  return Observable.merge(os);
}

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = NullPointerException.class)
public void mapReturnsNull() {
  just1.map(new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return null;
    }
  }).blockingSubscribe();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testTake2() {
  Observable<Integer> o = Observable.just(1, 2, 3, 4, 5);
  Iterable<String> it = Arrays.asList("a", "b", "c", "d", "e");
  SquareStr squareStr = new SquareStr();
  o.map(squareStr).zipWith(it, concat2Strings).take(2).subscribe(printer);
  assertEquals(2, squareStr.counter.get());
}

代码示例来源:origin: ReactiveX/RxJava

/**
 * We expect IllegalStateException to pass thru map.
 */
@Test(expected = IllegalStateException.class)
public void testErrorPassesThruMap2() {
  Observable.error(new IllegalStateException()).map(new Function<Object, Object>() {
    @Override
    public Object apply(Object i) {
      return i;
    }
  }).blockingSingle();
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<String> apply(final String s) {
    return Observable.just(s)
        .mergeWith(Observable.interval(10, TimeUnit.MILLISECONDS)
        .map(new Function<Long, String>() {
          @Override
          public String apply(Long i) {
            return s + " " + i;
          }
        })).take(250);
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Ignore("Not sure what this does")
@Test
public void addOnNextValueExceptionAdded() throws Exception {
  Observer<BadToString> observer = new BadToStringObserver();
  Observable.just(new BadToString(false))
      .map(new Function<BadToString, BadToString>() {
        @Override
        public BadToString apply(BadToString badToString) {
          throw new IllegalArgumentException("Failure while handling");
        }
      }).subscribe(observer);
}

代码示例来源:origin: ReactiveX/RxJava

@Ignore("Not sure what this does")
@Test
public void addOnNextValueExceptionNotAddedWithBadString() throws Exception {
  Observer<BadToString> observer = new BadToStringObserver();
  Observable.just(new BadToString(true))
      .map(new Function<BadToString, BadToString>() {
        @Override
        public BadToString apply(BadToString badToString) {
          throw new IllegalArgumentException("Failure while handling");
        }
      }).subscribe(observer);
}

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = IllegalArgumentException.class)
public void testTakeWithError() {
  Observable.fromIterable(Arrays.asList(1, 2, 3)).take(1)
  .map(new Function<Integer, Integer>() {
    @Override
    public Integer apply(Integer t1) {
      throw new IllegalArgumentException("some error");
    }
  }).blockingSingle();
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
  @Test
  public void source() {
    Observable<Integer> o = Observable.just(1);

    assertSame(o, ((HasUpstreamObservableSource<Integer>)o.map(Functions.<Integer>identity())).source());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void fusedReject() {
  TestObserver<Integer> to = ObserverFusion.newTest(QueueFuseable.ANY | QueueFuseable.BOUNDARY);
  Observable.range(1, 5)
  .map(Functions.<Integer>identity())
  .subscribe(to);
  ObserverFusion.assertFusion(to, QueueFuseable.NONE)
  .assertResult(1, 2, 3, 4, 5);
}

相关文章

Observable类方法