0
어떻게 영역에서 가장 자주 사용되는 값을 얻을 수 있습니까? 예를 들어Realm.Io Android 가장 자주 사용되는 값
:
Jack
Ryan
Lee
Jack
잭이 자주 찾는 값입니다.
어떻게 영역에서 가장 자주 사용되는 값을 얻을 수 있습니까? 예를 들어Realm.Io Android 가장 자주 사용되는 값
:
Jack
Ryan
Lee
Jack
잭이 자주 찾는 값입니다.
기억하십시오! Realm은 NoSQL 객체 저장소입니다! 이것은 가볍고 빠르며 객체가 저장하는 객체는 단지 Java 객체입니다. DB 엔진이 이와 같은 계산을 "최적화"할 필요가 없습니다. 다음과 같이 쉽게 할 수 있습니다.
public String getMostCommonName(Realm realm) {
RealmResults<Person> results = realm.where(Person.class).distinct("name");
long maxOccurances = 0;
Person mostCommon = null;
for (Person p : results) {
long n = realm.where(Person.class).equalTo("name", p.getName()).count();
if (n > maxOccurances) {
mostCommon = p;
maxOccurances = n;
}
}
return mostCommon.getName();
}