gpt4 book ai didi

java - 检查列表是否已更改

转载 作者:行者123 更新时间:2023-12-01 16:57:39 24 4
gpt4 key购买 nike

我想知道如何检查我的列表是否已更改?下面的编码器每 5 秒运行一次,如果新数据在数据库中实现,它将出现在 List 中。

List<String> list = new ArrayList<>();
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String Group_Names = document.getString("Title");
System.out.println("Groups: Listen to these - " + document.getString("Title"));
list.add(Group_Names);
}
} else {
System.out.println("Failed on: " + task.getException());
}

最佳答案

使用List的hashCode()检查列表是否被修改

List<String> list = new ArrayList<>();
if (task.isSuccessful()) {
int oldhashCode = list.hashCode();
for (QueryDocumentSnapshot document : task.getResult()) {
String Group_Names = document.getString("Title");
System.out.println("Groups: Listen to these - " + document.getString("Title"));
list.add(Group_Names);
}
int newhashCode = list.hashCode();
if(oldhashCode == newhashCode) { // hashcode is unique for very object in Java and fastest way to compare two object of same type
System.out.println("List is unchanged");
} else {
System.out.println("List is\changed");
}
} else {
System.out.println("Failed on: " + task.getException());
}

关于java - 检查列表是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61562001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com