RxJava 将 Single<String> 对象转换为 String class

RxJava convert Single<String> object to String class

是否可以将 Single 对象转换为 String class?

单个 <用户> stringSingle = Single.just(User.getName());

字符串 s = "";

s=stringSingle;

如何将 stringSingle 分配给 s 变量?

你可以有这样的东西。

import rx.Single;
import lombok.Getter;

public class TestClass {

  public static void main(String[] args) {
     Single<User> stringSingle = Single.just(new User("Peter", 9));
   final String[] s = new String[1];

   //Asynchronous and advisable 
   stringSingle.subscribe( result -> {
      s[0] = result.toString();
    });
    System.out.println(s[0]);
     
     //Blocking and not very advisible way of getting the value
    String s2 =   stringSingle.toBlocking().value().toString();
    System.out.println(s2);
  }
}

//Example of a possible User class to use.
class User{
  @Getter
  private String name;
  @Getter
  private int age ;

  public User( String nm,  int ag){
    name = nm;
    age = ag;
  }

  @Override
  public String toString(){
    return "name " + getName() + " and age " + getAge();
  }
}

结果是

name Peter and age 9
name Peter and age 9