简体   繁体   English

创建唯一对象列表

[英]Creating list of unique objects

I get a set of elements by parsing a html document. 我通过解析html文档得到了一组元素。 There is a possibility that the elements may contain duplicates. 元素可能包含重复项。 What is the best way to list only unique elements? 仅列出唯一元素的最佳方法是什么?

I come from C++ background and see a possibility of doing it using a set and custom equality operation. 我来自C ++背景,看到使用设置和自定义相等操作完成此操作的可能性。 However, not sure how to do it in Java. 但是,不确定如何用Java做到这一点。 Appreciate any code that would help me do it the right and efficient way. 感谢任何可以帮助我以正确,高效的方式进行操作的代码。

ArrayList<Element> values = new ArrayList<Element>();

// Parse the html and get the document
Document doc = Jsoup.parse(htmlDataInStringFormat);

// Go through each selector and find all matching elements
for ( String selector: selectors ) {

    //Find elements matching this selector
    Elements elements = doc.select(selector);

    //If there are no matching elements, proceed to next selector
    if ( 0 == elements.size() ) continue;

    for (Element elem: elements ){
        values.add(elem);
    }
}

if ( elements.size() > 0 ) {
    ????? // Need to remove duplicates here
}

java.util.HashSet will give you an Unordered set there are also other extensions of java.util.Set in the API that will give you ordered sets or concurrent behaviour if needed. java.util.HashSet将为您提供无序集,API中还有java.util.Set其他扩展,如果需要,它们将为您提供有序集或并发行为。

Depending upon what the class Element is you may additionally need to implement the equals and hashCode functions on it. 根据Element类的不同,您可能还需要在其上实现equals和hashCode函数。 as per comments by @musical_coder. 根据@musical_coder的评论。

eg: 例如:

Set<Element> set = new HashSet<Element>(elements);

in Order to provide an overridden equals method or Element I would create thin wrapper around the Element class for myself MyElement or something more sencibly named eg 为了提供一个覆盖的equals方法或Element,我将为我自己的MyElement或更明智地命名的东西(例如)围绕Element类创建一个薄包装器。

    public static class MyElement extends Element {

        private final Element element;

        public MyElement(Element element){
            this.element = element;
        }

        // OverRide equals and Hashcode
        // Delegate all other methods
    }

and pass that into the set, ok so now I'm hoping the class isn't final. 并将其传递到集合中,好的,现在我希望课程不是最后的课程。 Effectivly wrapp all your elements in this class. 有效地包装此类中的所有元素。 Ah ElementWrapper that is a better name. 嗯,那个ElementWrapper是个更好的名字。

将元素添加到java.util.HashSet ,它将仅包含唯一元素。

Use HashSet if you just want to avoid duplicate. 如果只想避免重复,请使用HashSet。 Use Tree set if you want ordering alongwith avoiding duplicates 如果要订购并避免重复,请使用树集

Additionally override the equals and hashCode method of Element 此外,重写Element的equals和hashCode方法

class Element {
...

public boolean equals(Object o) {
    if (! (o instanceof Element)) {
    return false;
}
Element other = (Element)o;
//compare the elements of  this and o like
if (o.a != this.a) { return false;}
...

}
...
public int hashCode() {
    //compute a value that is will return equal hash code for equal objects
}
}

While the answers posted work if there is a possibility to modify the element, I cannot do that. 如果有可能修改元素,则发布的答案会起作用,但我不能这样做。 I donot need a sorted set, hence here is the solution I found.. 我不需要排序集,因此这是我找到的解决方案。

TreeSet<Element> nt = new TreeSet<Element>(new Comparator<Element>(){
        public int compare(Element a, Element b){
            if ( a == b ) 
                return 0;
            if ( (a.val - b.val) > 0 )
                return 1;
            return -1;
        }
    });

for (Element elem: elements ){
    nt.add(elem);
}

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

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