Map
map是RxJava中最简单的一个变换操作符了, 它的作用就是对上游发送的每一个事件应用一个函数, 使得每一个事件都按照指定的函数去变化. 用事件图表示如下:
图中map中的函数作用是将圆形事件转换为矩形事件, 从而导致下游接收到的事件就变为了矩形.用代码来表示这个例子就是:
创建一个bean
package tongxunlu.com.myapplication;
/**
* Created by wangwei on 2018/8/30.
*/
public class TestBean {
private int userAccountType = -1; //账号类型。1:管理员账号 2:普通账号
private int roleID = -1;//账号所属角色ID
private int employeeID = -1;//账号对应的员工ID
private int businessType = -1;//账号对应的行业类型 1 美发 2足浴 3美容 5美甲
private String userAccountName;//账号名称
private int userAccountID = -1;//账号ID
private String roleName;//角色名称
public static final int BUS_TYPE_MEIFA = 1;
public static final int BUS_TYPE_ZUYU = 2;
public static final int BUS_TYPE_MEIRONG = 3;
public static final int BUS_TYPE_MEIJIA = 5;
public int getUserAccountType() {
return userAccountType;
}
public void setUserAccountType(int userAccountType) {
this.userAccountType = userAccountType;
}
public int getRoleID() {
return roleID;
}
public void setRoleID(int roleID) {
this.roleID = roleID;
}
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
public int getBusinessType() {
return businessType;
}
public void setBusinessType(int businessType) {
this.businessType = businessType;
}
public String getUserAccountName() {
return userAccountName;
}
public void setUserAccountName(String userAccountName) {
this.userAccountName = userAccountName;
}
public int getUserAccountID() {
return userAccountID;
}
public void setUserAccountID(int userAccountID) {
this.userAccountID = userAccountID;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
//是否有房态
public boolean hasRoom() {
if (businessType == 2) {
return true;
}
return false;
}
}
本地新建一个字符串
{
"businessType": 345,
"employeeID": 0,
"roleID": 2344,
"roleName": "我是名字",
"userAccountID": 4567897,
"userAccountName": "wangwei",
"userAccountType": 1
}
读取assert里面的字符串
//从资源文件中获取分类json
public static String getAssetsData(String path) {
String result = "";
try {
//获取输入流
InputStream mAssets = CoreApplication.getInstance().getAssets().open(path);
//获取文件的字节数
int lenght = mAssets.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据写入到字节数组中
mAssets.read(buffer);
mAssets.close();
result = new String(buffer);
return result;
} catch (IOException e) {
e.printStackTrace();
Log.e("fuck", e.getMessage());
return result;
}
}
通过代码开始订阅
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
Log.w(TAG, "Observable--" + Thread.currentThread().getName());
emitter.onNext(mUserInfoJson);
emitter.onComplete();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Function<String, TestBean>() {
@Override
public TestBean apply(String userString) throws Exception {
Log.w(TAG, "apply--" + Thread.currentThread().getName());
Gson gson = new Gson();
TestBean currentUseInfo = gson.fromJson(userString, TestBean.class);
return currentUseInfo;
}
}).subscribe(new Consumer<TestBean>() {
@Override
public void accept(TestBean s) throws Exception {
Log.w(TAG, "Observ--" + Thread.currentThread().getName());
Log.d(TAG, "accept---" + s.getUserAccountName());
}
});
在上游我们发送的是字符串类型, 而在下游我们接收的是TestBean类型, 中间起转换作用的就是map操作符, 运行结果为:
W/MainActivity: Observable--RxCachedThreadScheduler-1
W/MainActivity: apply--main
W/MainActivity: apply--userString{
"businessType": 345,
"employeeID": 0,
"roleID": 2344,
"roleName": "我是名字",
"userAccountID": 4567897,
"userAccountName": "wangwei",
"userAccountType": 1
}
W/MainActivity: Observ--main
D/MainActivity: accept---wangwei
通过Map, 可以将上游发来的事件转换为任意的类型, 可以是一个Object, 也可以是一个集合, 如此强大的操作符快去试试吧。