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

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

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

Observable.sorted介绍

[英]Returns an Observable that emits the events emitted by 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 calling sorted with long, non-terminating or infinite sources might cause OutOfMemoryError Scheduler: sorted does not operate by default on a particular Scheduler.
[中]返回一个可观察对象,该对象按排序顺序发出源ObservableSource发出的事件。ObservableSource发出的每个项必须实现与序列中所有其他项的可比性。
如果此可观测项发出的任何项没有实现与此可观测项发出的所有其他项的可比性,则不会发出任何项,并且序列以ClassCastException终止。
请注意,使用长、非终止或无限源调用sorted可能会导致OutOfMemoryError调度程序:sorted默认情况下不会在特定调度程序上运行。

代码示例

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

@Test
public void sorted() {
  Observable.just(5, 1, 2, 4, 3).sorted()
  .test()
  .assertResult(1, 2, 3, 4, 5);
}

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

@Test
public void sortedComparator() {
  Observable.just(5, 1, 2, 4, 3).sorted(new Comparator<Integer>() {
    @Override
    public int compare(Integer a, Integer b) {
      return b - a;
    }
  })
  .test()
  .assertResult(5, 4, 3, 2, 1);
}

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

@Override
protected Single<List<Repository>> loadData(boolean forceRefresh) {
  User org = this.org.get();
  if (org == null) {
    return Single.just(Collections.emptyList());
  }
  return Single.fromCallable(() -> cache.getRepos(org, forceRefresh))
      .flatMap(repos -> Observable.fromIterable(repos)
          .sorted(recentRepos)
          .toList());
}

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

@Override
protected Single<List<IssueFilter>> loadData(boolean forceRefresh) {
  return Single.fromCallable(() -> new ArrayList<>(cache.getIssueFilters()))
      .flatMap(filters -> Observable.fromIterable(filters)
          .sorted(FilterListFragment.this)
          .toList());
}

代码示例来源:origin: silentbalanceyh/vertx-zero

static JsonArray sort(
      final JsonArray array,
      final boolean immutable
  ) {
    final JsonArray result = immutable ? array.copy() : array;
    final JsonArray dis = new JsonArray();
    Observable.fromIterable(result)
        .sorted()
        .subscribe(dis::add).dispose();
    return result.clear().addAll(dis);
  }
}

代码示例来源:origin: cn.vertxup/vertx-co

static JsonArray sort(
      final JsonArray array,
      final boolean immutable
  ) {
    final JsonArray result = immutable ? array.copy() : array;
    final JsonArray dis = new JsonArray();
    Observable.fromIterable(result)
        .sorted()
        .subscribe(dis::add).dispose();
    return result.clear().addAll(dis);
  }
}

代码示例来源:origin: LRH1993/LiveCircle

@Override
  public Observable<NewsInfo> requestNewsList(String newsType, final String id, int startPage) {
    RetrofitClient retrofitClient = RetrofitClient.getInstance(AppApplication.getAppContext(), ApiService.NEWS_BASE_URL);
    ApiService api = retrofitClient.create(ApiService.class);
    return api.getNewsList(newsType,id,startPage).flatMap(new Function<Map<String, List<NewsInfo>>, ObservableSource<NewsInfo>>() {
      @Override
      public ObservableSource<NewsInfo> apply(Map<String, List<NewsInfo>> map) throws Exception {
        //房产类型的信息
        if (id.endsWith(ApiConstants.HOUSE_ID)) {
          return Observable.fromIterable(map.get("北京"));
        }else{
          return Observable.fromIterable(map.get(id));
        }
      }
    }).map(new Function<NewsInfo, NewsInfo>() {//转换时间格式
      @Override
      public NewsInfo apply(NewsInfo info) throws Exception {
        String ptime = TimeUtil.formatDate(info.getPtime());
        info.setPtime(ptime);
        return info;
      }
    }).distinct().sorted(new Comparator<NewsInfo>() {
      @Override
      public int compare(NewsInfo info, NewsInfo t1) {
        return t1.getPtime().compareTo(info.getPtime());
      }
    }).compose(RetrofitClient.schedulersTransformer);//线程调度
  }
}

代码示例来源:origin: com.firstdata.clovergo/domain

public Observable<Tips> getObservable() {
    repository.log("get tips");
    final EmployeeMerchant employeeMerchant = repository.retrieveFromCache(UseCaseConstants.EMPLOYEE_MERCHANT);
    if (employeeMerchant == null) {
      return Observable.error(new Error(ERROR_CODE_NO_EMPLOYEE_LOGGED_IN, ERROR_MESSAGE_NO_EMPLOYEE_LOGGED_IN));
    } else {
       return repository.
           getTips(employeeMerchant.getEmployee().getId(), employeeMerchant.getMerchant().getId(), employeeMerchant.getEmployee().getSessionToken()).sorted((tips, t1) -> {
             if (tips.getPercentage() == t1.getPercentage())
               return 0;
             else if (tips.getPercentage() < t1.getPercentage())
               return -1;
             else
               return 1;
           });
    }
  }
}

相关文章

Observable类方法