1

I have a pre-populated realm database. I have the following object in there :

public class RLMFormula extends RealmObject {
    @PrimaryKey
    private String idHighTech;
    private String label;

    public String getIdhightech() { return idHighTech; }

    public void setIdhightech(String idHighTech) { this.idHighTech = idHighTech; }

}

I would like to extract all idHighTech in an ArrayList, for the moment I do it like that :

public ArrayList<String> getAllFormulaID() {
    RealmResults<RLMFormula> rlmFormula = realm.where(RLMFormula.class).findAll();

    ArrayList<String> formulaListId = new ArrayList<>();
    for (int i = 0; i < rlmFormula.size(); i++) {
        formulaListId.add(rlmFormula.get(i).getIdhightech());
    }

    return formulaListId;
}

Is there something more efficient and maybe faster ?

Yohann L.
  • 1,262
  • 13
  • 27
  • why do you want to make it faster in the first place? – Tim Feb 02 '18 at 15:24
  • There is no real needs to make it faster, I just wanted to know if there is a better way to do it – Yohann L. Feb 02 '18 at 15:27
  • that depends on what you consider "better", but this looks good already. You could make it fancy with java8 streams or rxjava, but not sure why you would – Tim Feb 02 '18 at 15:31
  • Well when I said better, I would like to say an existing function given from realm like `ArrayList list = rlmFormula.findAll("idHighTech")` and return the ArrayList with all idHighTech. I'alm a beginner with realm database and I don't know all functions and tricks – Yohann L. Feb 02 '18 at 15:36
  • realm does not have mapping functionality built in, but that's the term you're looking for. Hence I named streams and rxjava, they can do this kind of stuff for you – Tim Feb 02 '18 at 15:37
  • I'll look into it, thank you ! – Yohann L. Feb 02 '18 at 15:41

0 Answers0