Java 查找firebase上是否存在列表项?

Java 查找firebase上是否存在列表项?,java,android,database,sqlite,firebase,Java,Android,Database,Sqlite,Firebase,我已将列表存储在firebase上:- names : [ "john" , "jack" , "jason" , "jill" , "travis","alice"] 我有一份清单: String[] names = {"john" , "jack" , "jane" , "jason"}; List<String> list = new ArrayList<>(Arrays.asList(names)); String[]name={“约翰”

我已将列表存储在firebase上:-

names : [ "john" , "jack" , "jason" ,
          "jill" , "travis","alice"]
我有一份清单:

String[] names = {"john" , "jack" , "jane" , "jason"};

List<String> list = new ArrayList<>(Arrays.asList(names));
String[]name={“约翰”、“杰克”、“简”、“杰森”};
List List=newarraylist(Arrays.asList(names));

如果不从firebase获取列表,我如何才能发现john、jack和jason在firebase上存在,而jane却不存在?

firebase数据库没有exists操作。因此,知道项目是否存在的唯一方法是检索它

也就是说,处理这种情况有几种选择。但我首先要建议您修复数据结构:任何时候您有一个数组并尝试对其执行包含操作时,您都应该使用一个集合。我不再重复我自己的话,而是在这里参考我的答案:。现在就去读吧。。。你回来时我会在这里

现在,您已经阅读了集合与阵列的相关内容,您将看到您的数据更好地存储为:

names : { 
  "john": true,
  "jack":true , 
  "jason": true,
  "jill": true,
  "travis": true,
  "alice": true
}
好多了。:-)

现在回到你的问题:你必须检索一个项目来检查它是否存在。我可以想出两种方法:要么得到整个列表,要么得到每个单独的项目

检索整个列表,然后对特定项使用
DataSnapshot.exists()

ref.child("names").addSingleValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        if (snapshot.child("john").exists() && !snapshot.child("jane").exists()) {
            ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
        // ...
    }
});
另一种方法是检索单个项目:

ref.child("names/john").addSingleValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        if (snapshot.exists()) {
            ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
        // ...
    }
});

哪一个最有效很大程度上取决于您希望列表中有多少名字。

谢谢。。。因为修复列表现在看起来更加结构化。但我在firebase上有一个90k-95k元素的列表,我不认为检索这么大的列表只检查5个元素是个好主意。firebase上的列表和客户端的列表也可以更改。所以,每当列表发生任何更改时,我都必须从firebase检索列表。有什么建议吗?有这么多名字,你最好不要检索整个列表。不过,替代方案仍应适用。