如何迭代所有数据SQLite并完全分配给Hashmap?

时间:2019-03-19 12:06:14

标签: android sqlite hashmap

在我的SQLite中,实际上有375个数据,但是当我尝试迭代该数据并将其成功分配给hashmap时,仅分配了226个数据,下面的代码有什么问题吗?

class PublishersState {
  val publishersList = Collections.synchronizedList<Flux<String>>(mutableListOf()) // adding sync list for storing publishers 
  val outputProcessor = DirectProcessor.create<String>()

  fun addNewPublisher(publisher: Flux<String>) {
    val cached = publisher.cache(1) // caching the last item for a new publisher
    publishersList.add(cached)
    cached.subscribe(outputProcessor)
  }

  fun getAllPublishersState(): Flux<String> = publishersList
    .toFlux()
    .reduce(outputProcessor as Flux<String>) { acc, flux -> acc.mergeWith(flux.take(1)) } // merging the last item of each publisher with outputProcessor 
    .flatMapMany { it }
}

1 个答案:

答案 0 :(得分:0)

您有多个条目,其中一个KEY_LANG。您可以在地图值中存储STRING而不是一个STRING的列表

public static HashMap<String, ArrayList<String>> getLanguage() {
    HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
    Cursor cursor = database.query(DB_TABLE,
            null,
            null,
            null,
            null,
            null,
            _ID + " ASC",
            null);
    //cursor.moveToFirst();
    System.out.println("Cursor count" + cursor.getCount());
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        String key = cursor.getString(cursor.getColumnIndex(KEY_LANG));
        String value = cursor.getString(cursor.getColumnIndex(STRING));
        if (hashMap.containsKey(key)) {
            hashMap.get(key).add(value);
        } else {
            ArrayList<String> list = new ArrayList<>();
            list.add(value);
            hashMap.put(key, list);
        }
    }
    return hashMap;
}