I have a SplayTreeSet of the objects ChatRoomListModel. I'd like to order my set based on the objects DateTime createTime value.
I'm not sure where I'm going wrong because there's duplicate items being added item.
I later down the line perform a _latestMessages.add(newMessage) and it's not actually calling the overloads and there's duplicates being added.
I tested by using _latestMessage.contains(newMessageButSameChatRoomId), it returns false.
When I perform _latestMessage.toSet() every duplicate goes away.
How can I get SplayTreeSet to use my overloading equals?
Thanks!
ObservableSet<ChatRoomListModel> _latestMessages = ObservableSet.splayTreeSetFrom(
ObservableSet(),
compare: (a, b) => b.compareTo(a),
);
The ChatRoomListModel model has the following methods and overloads:
int compareTo(ChatRoomListModel other){
return messagesModel.createTime.compareTo(other.messagesModel.createTime);
}
ChatRoomListModel copyWith({
String? firstName,
String? otherUserId,
MessagesModel? messagesModel,
}) {
return ChatRoomListModel(
firstName: firstName ?? this.firstName,
otherUserId: otherUserId ?? this.otherUserId,
messagesModel: messagesModel ?? this.messagesModel,
);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ChatRoomListModel &&
runtimeType == other.runtimeType &&
messagesModel.chatRoomId == other.messagesModel.chatRoomId;
@override
int get hashCode => messagesModel.chatRoomId.hashCode;
Your issue is that you have two completely different notions of what it means for two
ChatRoomListModelobjects to be "equal". You provide bothcompareToandoperator ==implementations, but they consider different sets of properties, socompareTocan return 0 whenoperator ==returns false, which is confusing at best.SplayTreeMapconsiders onlycompareTo, notoperator ==. From theSplayTreeSetdocumentation:I'm presuming what you call "duplicates" are elements that have equal
chatRoomIds but that have different creation times, and creation times are the only things that yourSplayTreeSetcares about.If your goal is to maintain only the latest message per
chatRoomId, you need to maintain a data structure that uses thechatRoomId(aString) as a key. The natural collection for that would be aMap<String, ChatRoomListModel>. (Since theChatRoomListModelknows its ownchatRoomId, it also could just be aHashSetwith explicitequalsandhashCodecallbacks.)If you additionally want to keep messages in chronological order, you either will need to explicitly sort them afterward or maintain a separate data structure that keeps them in chronological order. You could continue using a
SplayTreeSetfor that. Basically before you add any entry to theSplayTreeSet, check theMapfirst to see if an existing entry for thatchatRoomId.I don't fully understand your data structures, but here's an example that you presumably can adapt: