私はRXJavaを初めて使用します。初期化でほぼ同じことを行う必要がある複数のサブジェクトを定義していますが、それらのジェネリック型は異なります。現在、私のソリューションは次のようになっています。
BehaviorSubject<PepPoint> mUpdateSubject = BehaviorSubject.create();
BehaviorSubject<String> mMessageSubject = BehaviorSubject.create();
mMessageSubject.onNext(mMessage);//type string
mMessageSubject.distinctUntilChanged().skip(1).debounce(5, TimeUnit.SECONDS).sample(1, TimeUnit.MINUTES).subscribe(this::onChanged);
BehaviorSubject<Double> mTriggerRadiusSubject = BehaviorSubject.create();
mTriggerRadiusSubject.onNext(mTriggerRadius);//type double
mMessageSubject.distinctUntilChanged().skip(1).debounce(5, TimeUnit.SECONDS).sample(1, TimeUnit.MINUTES).subscribe(this::onChanged);
BehaviorSubject<SimpleLocation> observableLocation = BehaviorSubject.create();
observableLocation.onNext(mLocation);//type Location
observableLocation.distinctUntilChanged().skip(1).debounce(5, TimeUnit.SECONDS).sample(1, TimeUnit.MINUTES).subscribe(this::onChanged);
/*And same for many several other variables*/
繰り返しが多いことがわかります。したがって、私が達成したいことは、次のようになることが望ましいです。
{
BehaviorSubject<PepPoint> mUpdateSubject = BehaviorSubject.create();
Map<Object, BehaviorSubject> subjectMap = new TreeMap<>();
subjectMap.put(mMessage, BehaviorSubject.<String>create());
subjectMap.put(mTriggerRadius, BehaviorSubject.<Double>create());
subjectMap.put(mLocation, BehaviorSubject.<Location>create());
subjectMap.put(mPreconditions, BehaviorSubject.<List<String>>create());
subjectMap.put(mLanguage, BehaviorSubject.<String>create());
for(Map.Entry<Object, BehaviorSubject> entry:subjectMap.entrySet()){
entry.getValue().onNext(entry.getKey());
mMessageSubject
.distinctUntilChanged()
.skip(1)
.debounce(5,TimeUnit.SECONDS)
.subscribe(this::onChange);
}
}
これが機能しない理由がわかりません。私も試してみ
Map<? super Object, BehaviorSubject<? super Object>> subjectMap = new TreeMap<>();
ました ありがとう