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

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

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

Observable.toSortedList介绍

[英]Returns a Single that emits a list that contains the items emitted by the finite source ObservableSource, in a sorted order. Each item emitted by the ObservableSource must implement Comparable with respect to all other items in the sequence.

If any item emitted by this Observable does not implement Comparable with respect to all other items emitted by this Observable, no items will be emitted and the sequence is terminated with a ClassCastException.

Note that this operator requires the upstream to signal onComplete for the accumulated list to be emitted. Sources that are infinite and never complete will never emit anything through this operator and an infinite source may lead to a fatal OutOfMemoryError. Scheduler: toSortedList does not operate by default on a particular Scheduler.
[中]返回一个单子,该单子发出一个列表,其中包含有限源ObserveSource按排序顺序发出的项。ObservableSource发出的每个项必须实现与序列中所有其他项的可比性。
如果此可观测项发出的任何项没有实现与此可观测项发出的所有其他项的可比性,则不会发出任何项,并且序列以ClassCastException终止。
请注意,该运算符要求上游发出“完成”信号,以发出累积列表。无限且永远不完整的源永远不会通过该运算符发出任何信息,而无限源可能会导致致命的OutOfMemory错误。Scheduler:toSortedList默认情况下不会在特定的计划程序上运行。

代码示例

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

@Test(expected = NullPointerException.class)
public void toSortedListNull() {
  just1.toSortedList(null);
}

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

@Test
public void testSortedList() {
  Observable<Integer> w = Observable.just(1, 3, 2, 5, 4);
  Single<List<Integer>> single = w.toSortedList();
  SingleObserver<List<Integer>> observer = TestHelper.mockSingleObserver();
  single.subscribe(observer);
  verify(observer, times(1)).onSuccess(Arrays.asList(1, 2, 3, 4, 5));
  verify(observer, Mockito.never()).onError(any(Throwable.class));
}

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

/**
 * Returns a Single that emits a list that contains the items emitted by the finite source ObservableSource, in a
 * sorted order. Each item emitted by the ObservableSource must implement {@link Comparable} with respect to all
 * other items in the sequence.
 *
 * <p>If any item emitted by this Observable does not implement {@link Comparable} with respect to
 *             all other items emitted by this Observable, no items will be emitted and the
 *             sequence is terminated with a {@link ClassCastException}.
 * <p>
 * <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/toSortedList.2.png" alt="">
 * <p>
 * Note that this operator requires the upstream to signal {@code onComplete} for the accumulated list to
 * be emitted. Sources that are infinite and never complete will never emit anything through this
 * operator and an infinite source may lead to a fatal {@code OutOfMemoryError}.
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code toSortedList} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 * @return a Single that emits a list that contains the items emitted by the source ObservableSource in
 *         sorted order
 * @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX operators documentation: To</a>
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<List<T>> toSortedList() {
  return toSortedList(Functions.naturalOrder());
}

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

@Test
public void testSortedListObservable() {
  Observable<Integer> w = Observable.just(1, 3, 2, 5, 4);
  Observable<List<Integer>> observable = w.toSortedList().toObservable();
  Observer<List<Integer>> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer, times(1)).onNext(Arrays.asList(1, 2, 3, 4, 5));
  verify(observer, Mockito.never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

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

@Test
public void testSortedListWithCustomFunction() {
  Observable<Integer> w = Observable.just(1, 3, 2, 5, 4);
  Single<List<Integer>> single = w.toSortedList(new Comparator<Integer>() {
    @Override
    public int compare(Integer t1, Integer t2) {
      return t2 - t1;
    }
  });
  SingleObserver<List<Integer>> observer = TestHelper.mockSingleObserver();
  single.subscribe(observer);
  verify(observer, times(1)).onSuccess(Arrays.asList(5, 4, 3, 2, 1));
  verify(observer, Mockito.never()).onError(any(Throwable.class));
}

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

@SchedulerSupport(SchedulerSupport.NONE)
public final Single<List<T>> toSortedList(int capacityHint) {
  return toSortedList(Functions.<T>naturalOrder(), capacityHint);

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

@SuppressWarnings("unchecked")
@Test
public void toSortedListCapacity() {
  Observable.just(5, 1, 2, 4, 3).toSortedList(4)
  .test()
  .assertResult(Arrays.asList(1, 2, 3, 4, 5));
}

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

@Test
public void testWithFollowingFirst() {
  Observable<Integer> o = Observable.just(1, 3, 2, 5, 4);
  assertEquals(Arrays.asList(1, 2, 3, 4, 5), o.toSortedList().blockingGet());
}

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

@SuppressWarnings("unchecked")
@Test
public void toSortedListComparatorCapacity() {
  Observable.just(5, 1, 2, 4, 3).toSortedList(new Comparator<Integer>() {
    @Override
    public int compare(Integer a, Integer b) {
      return b - a;
    }
  }, 4)
  .test()
  .assertResult(Arrays.asList(5, 4, 3, 2, 1));
}

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

@Test
public void testSortedListWithCustomFunctionFlowable() {
  Observable<Integer> w = Observable.just(1, 3, 2, 5, 4);
  Observable<List<Integer>> observable = w.toSortedList(new Comparator<Integer>() {
    @Override
    public int compare(Integer t1, Integer t2) {
      return t2 - t1;
    }
  }).toObservable();
  Observer<List<Integer>> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer, times(1)).onNext(Arrays.asList(5, 4, 3, 2, 1));
  verify(observer, Mockito.never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

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

@Test
public void testWithFollowingFirstObservable() {
  Observable<Integer> o = Observable.just(1, 3, 2, 5, 4);
  assertEquals(Arrays.asList(1, 2, 3, 4, 5), o.toSortedList().toObservable().blockingFirst());
}

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

@SuppressWarnings("unchecked")
@Test
public void toSortedListCapacityObservable() {
  Observable.just(5, 1, 2, 4, 3).toSortedList(4).toObservable()
  .test()
  .assertResult(Arrays.asList(1, 2, 3, 4, 5));
}

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

@SuppressWarnings("unchecked")
@Test
public void toSortedListComparatorCapacityObservable() {
  Observable.just(5, 1, 2, 4, 3).toSortedList(new Comparator<Integer>() {
    @Override
    public int compare(Integer a, Integer b) {
      return b - a;
    }
  }, 4).toObservable()
  .test()
  .assertResult(Arrays.asList(5, 4, 3, 2, 1));
}

代码示例来源:origin: redisson/redisson

/**
 * Returns a Single that emits a list that contains the items emitted by the finite source ObservableSource, in a
 * sorted order. Each item emitted by the ObservableSource must implement {@link Comparable} with respect to all
 * other items in the sequence.
 *
 * <p>If any item emitted by this Observable does not implement {@link Comparable} with respect to
 *             all other items emitted by this Observable, no items will be emitted and the
 *             sequence is terminated with a {@link ClassCastException}.
 * <p>
 * <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/toSortedList.2.png" alt="">
 * <p>
 * Note that this operator requires the upstream to signal {@code onComplete} for the accumulated list to
 * be emitted. Sources that are infinite and never complete will never emit anything through this
 * operator and an infinite source may lead to a fatal {@code OutOfMemoryError}.
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code toSortedList} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 * @return a Single that emits a list that contains the items emitted by the source ObservableSource in
 *         sorted order
 * @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX operators documentation: To</a>
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<List<T>> toSortedList() {
  return toSortedList(Functions.naturalOrder());
}

代码示例来源:origin: redisson/redisson

@SchedulerSupport(SchedulerSupport.NONE)
public final Single<List<T>> toSortedList(int capacityHint) {
  return toSortedList(Functions.<T>naturalOrder(), capacityHint);

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

@Test
public void testSortedList() {
  Comparator<Media> sortFunction = new Comparator<Media>() {
    @Override
    public int compare(Media t1, Media t2) {
      return 1;
    }
  };
  // this one would work without the covariance generics
  Observable<Media> o = Observable.just(new Movie(), new TVSeason(), new Album());
  o.toSortedList(sortFunction);
  // this one would NOT work without the covariance generics
  Observable<Movie> o2 = Observable.just(new Movie(), new ActionMovie(), new HorrorMovie());
  o2.toSortedList(sortFunction);
}

代码示例来源:origin: pockethub/PocketHub

/**
 * Create dialog helper to display milestones
 *
 * @param activity
 * @param requestCode
 * @param repository
 */
public MilestoneDialog(final BaseActivity activity,
    final int requestCode, final Repository repository) {
  this.activity = activity;
  this.requestCode = requestCode;
  GitHubRequest<Response<Page<Milestone>>> gitHubRequest = page -> ServiceGenerator
      .createService(activity, IssueMilestoneService.class)
      .getRepositoryMilestones(repository.owner().login(), repository.name(),
          "open", page);
  milestoneSingle = RxPageUtil.getAllPages(gitHubRequest, 1)
      .flatMap(page -> Observable.fromIterable(page.items()))
      .toSortedList((m1, m2) -> CASE_INSENSITIVE_ORDER.compare(m1.title(), m2.title()))
      .compose(RxProgress.bindToLifecycle(activity, R.string.loading_milestones))
      .cache();
}

代码示例来源:origin: pockethub/PocketHub

/**
 * Create dialog helper to display assignees
 *
 * @param activity
 * @param requestCode
 * @param repository
 */
public AssigneeDialog(final BaseActivity activity,
    final int requestCode, final Repository repository) {
  this.activity = activity;
  this.requestCode = requestCode;
  GitHubRequest<Response<Page<User>>> gitHubRequest = page -> ServiceGenerator
      .createService(activity, IssueAssigneeService.class)
      .getAssignees(repository.owner().login(), repository.name(), page);
  assigneeSingle = RxPageUtil.getAllPages(gitHubRequest, 1)
      .flatMap(page -> Observable.fromIterable(page.items()))
      .toSortedList((o1, o2) -> CASE_INSENSITIVE_ORDER.compare(o1.login(), o2.login()))
      .compose(RxProgress.bindToLifecycle(activity, R.string.loading_collaborators))
      .cache();
}

代码示例来源:origin: pockethub/PocketHub

/**
 * Create dialog helper to display labels
 *
 * @param activity
 * @param requestCode
 * @param repository
 */
public LabelsDialog(final BaseActivity activity,
    final int requestCode, final Repository repository) {
  this.activity = activity;
  this.requestCode = requestCode;
  GitHubRequest<Response<Page<Label>>> gitHubRequest = page -> ServiceGenerator
      .createService(activity, IssueLabelService.class)
      .getRepositoryLabels(repository.owner().login(), repository.name(), page);
  labelsSingle = RxPageUtil.getAllPages(gitHubRequest, 1)
      .flatMap(page -> Observable.fromIterable(page.items()))
      .toSortedList((o1, o2) -> CASE_INSENSITIVE_ORDER.compare(o1.name(), o2.name()))
      .compose(RxProgress.bindToLifecycle(activity, R.string.loading_labels))
      .cache();
}

代码示例来源:origin: pockethub/PocketHub

/**
 * Create dialog helper to display refs
 *
 * @param activity
 * @param requestCode
 * @param repository
 */
public RefDialog(final BaseActivity activity,
    final int requestCode, final Repository repository) {
  this.activity = activity;
  this.requestCode = requestCode;
  GitHubRequest<Response<Page<GitReference>>> gitHubRequest = page -> ServiceGenerator
      .createService(activity, GitService.class)
      .getGitReferences(repository.owner().login(), repository.name(), page);
  refSingle = RxPageUtil.getAllPages(gitHubRequest, 1)
      .flatMap(page -> Observable.fromIterable(page.items()))
      .filter(RefUtils::isValid)
      .toSortedList((o1, o2) -> CASE_INSENSITIVE_ORDER.compare(o1.ref(), o2.ref()))
      .compose(RxProgress.bindToLifecycle(activity, R.string.loading_refs))
      .cache();
}

相关文章

Observable类方法