简体   繁体   English

如何从自定义对象中删除树集中的重复项

[英]How do I remove duplicates from tree set with Custom Objects

I have an ArrayList (cntct) of ParseMessage's. 我有一个ParseMessage的ArrayList(cntct)。 ParseMessage has the ParseMessage有

private long dateSent;
private String contact;
private String body;

with the getters and setters generated by eclipse. 与eclipse生成的getter和setter。 I am trying to get the most recent message from each contact. 我正在尝试从每个联系人那里获取最新消息。 So I decided to do it as such 所以我决定这样做

SortedSet<ParseMessage> cntctList = new TreeSet<ParseMessage>(new Comparator<ParseMessage>() {
@Override
public int compare(ParseMessage o1, ParseMessage o2)
{   
    if(o1 == null || o2 == null)
        return 0;
    if(o1.getContact().equals(o2.getContact()))
        return 0;
    if(o1.getDateSent() <= o2.getDateSent())
        return 1;
    return -1;
}           
});
cntctList.addAll(cntct);

I seemed to have missed something in this though, as I am still getting a limited number of duplicates. 我似乎错过了一些东西,因为我仍然得到有限数量的重复。 I am using maybe 100 messages with 5 contacts and the set ends up with a size of 7 我使用可能有100条消息和5个联系人,该集合最终大小为7

EDIT: 编辑:

ParseMessage does override .equals and .hasCode As such ParseMessage会覆盖.equals和.hasCode

@Override
public int hashCode() {
    return getContact().hashCode();
}

@Override
public boolean equals(Object e) {
    if(!(e instanceof ParseMessage))
    {
        return false;
    }

    return ((ParseMessage) e).getContact().equals(getContact());
}

END: 结束:

Also this is for a web based call. 这也适用于基于网络的通话。 If anyone see's a way to make this faster then I would love to hear ideas. 如果有人看到这样做的方法更快,那么我很乐意听到想法。

The code in the question doesn't work because the compare method violates the rules, eg 问题中的代码不起作用,因为compare方法违反了规则,例如

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0 . 实现者还必须确保关系是传递的: ((compare(x, y)>0) && (compare(y, z)>0))表示compare(x, z)>0

For example: 例如:
compare(A1, B2) would return <0 because A != B && 1 < 2 compare(A1, B2)将返回<0,因为A != B && 1 < 2
compare(B2, A3) would return <0 because B != A && 2 < 3 compare(B2, A3)将返回<0,因为B != A && 2 < 3
compare(A1, A3) would return 0 because A == A , but the rules require it to return <0 compare(A1, A3)将返回0,因为A == A ,但规则要求它返回<0

When the rules are broken, the result is non-deterministic. 当规则被破坏时,结果是不确定的。


To build a collection of ParseMessage with only the most recent message from each contact, you should create a Map . 要构建ParseMessage的集合,只包含每个联系人的最新消息,您应该创建一个Map

List<ParseMessage> cntct = /*...*/;

// Build map of contact to most recent message
Map<String, ParseMessage> cntctMap = cntct.stream().collect(Collectors.toMap(
        ParseMessage::getContact,
        Function.identity(),
        (a, b) -> a.getDateSent() >= b.getDateSent() ? a : b
));

If a collection of messages is needed, call values(): 如果需要消息集合,请调用值():

Collection<ParseMessage> cntctList = cntctMap.values();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM