【问题标题】:Multiple Field Collection Sort多字段集合排序
【发布时间】:2013-07-09 05:21:26
【问题描述】:

我有一个类 Arraylist 包含值 字符串字、字符串扩展字、双重置信、双重支持

我想根据置信度对arraylist进行排序,然后根据支持度。

我已经成功地根据置信度对数组列表进行了排序,但是我未能创建一种基于支持对数组列表进行排序的新方法

这是我根据信心对其进行排序的代码

public class ExpandedTerm implements Comparable<ExpandedTerm> {
String word;
String expandedWord;
double support;
double confidence;

public ExpandedTerm (String word,String expandedWord, double confidence,double support){
    this.word = word;
    this.expandedWord = expandedWord;
    this.support = support;
    this.confidence = confidence;
}

public String getWord(){
    return word;
}

public String expandedWord(){
    return expandedWord;
}

public Double getSupport(){
    return support;
}

public Double getConfidence(){
    return confidence;
}

@Override
public int compareTo(ExpandedTerm conf) {
    return new Double(this.confidence).compareTo(new Double(conf.confidence));
}

我未能创建另一种方法,如 compareTo,根据支持值对其进行排序。 如何先按置信度排序,再用另一种方法按支持度排序?

【问题讨论】:

标签: java class sorting arraylist


【解决方案1】:

用户比较器。作为可比提供的功能,可以在单一类型的基础上进行排序。 这是您找到何时使用可比较和 comapartor 的链接
http://iandjava.blogspot.in/2012/10/comparable-and-comparator.html

使用多个比较器

  • 信心满满

    public class ConfidanceComparator implements Comparator<ExpandedTerm> {
        @Override
        public int compare(final ExpandedTerm  o1, final ExpandedTerm  o2) {
            return new Double(o1.confidence).compareTo(new Double(o2.confidence));
        }
    }
  • 一个支持

    public class SupportComparator implements Comparator<ExpandedTerm> {
        @Override
        public int compare(final ExpandedTerm  o1, final ExpandedTerm  o2) {
            return new Double(o1.support).compareTo(new Double(o2.support));
        }
    }

并使用Collections.sort(&lt;List&gt;, &lt;comparator&gt;) adn,您将获得所需的列表。

仅当您希望基于信任或支持进行排序时,才需要这样做。
但是,如果您需要,则首先按信任度排序,如果信任度相等,则检查支持度。那么可比较就足够了,并且是

public int compareTo(ExpandedTerm conf) {
    int compare = new Double(this.confidence).compareTo(new Double(conf.confidence));

    if (compare == 0) {
        compare = new Double(this.support).compareTo(new Double(conf.support));
    }
    return compare;
}

【讨论】:

  • 关于这个答案的一切都是不正确的。没有理由不能在这里使用Comparable。 -1 是一个糟糕的答案。
  • @MattBall Comparable 没有被使用,因为他想在不同的基础上进行比较,有时是出于信任,有时是出于支持
  • 我同意@MattBall,因为在实现Comparable 时,您可以编写多个类型基础比较。使用它的缺点是您无法更改该比较方法,以防您需要使用其他比较算法。
  • 好的,我知道你现在得到了什么。和我的解读不同。很好,但是你的泛型在哪里?
  • 现在在你最后一次编辑后它可以编译,但仍然不能直接回答 OP 问题。
【解决方案2】:

为您的 compareTo 方法尝试以下代码:

@Override
public int compareTo(ExpandedTerm other) {
    Double thisConfidence = new Double(getConfidence());
    Double otherConfidence = new Double(other.getConfidence());
    int compare = thisConfidence.compareTo(otherConfidence);

    if (compare == 0) {
        Double thisSupport = new Double(getSupport());
        Double otherSupport = new Double(other.getSupport());
        compare = thisSupport.compareTo(otherSupport);
    }
    return compare;
}

如果“信心”相等,基本上只比较“支持”。

【讨论】:

  • 实际上,我必须根据置信度对数组列表进行排序。 1.按置信度排序>>>>然后arraylist排序2.按支持排序>>>>然后新结果arraylist排序
【解决方案3】:

我看到你的回复是你想要排序一次然后再排序一次,所以我假设你想在排序时添加一个自定义比较器。这是你要找的吗?

public static void main(String[] args) {
    ExpandedTerm term1 = new ExpandedTerm("a", "b", 1, 4);
    ExpandedTerm term2 = new ExpandedTerm("c", "d", 3, 2);

    List<ExpandedTerm> list = new ArrayList();
    list.add(term1);
    list.add(term2);

    Collections.sort(list);
    System.out.println(list);

    Collections.sort(list, new Comparator<ExpandedTerm>() {
        @Override
        public int compare(ExpandedTerm o1, ExpandedTerm o2) {
            return new Double(o2.confidence).compareTo(new Double(o1.confidence));
        }
    });
    System.out.println(list);
}

这是输出

[ExpandedTerm@2eeb3c84, ExpandedTerm@55d2162c]
[ExpandedTerm@55d2162c, ExpandedTerm@2eeb3c84]

其他一些提示:确保为 ExpandedTerm 实现了 toString()、hashCode() 和 equals() 函数。这些对于调试以及在其他集合(如 HashMap 等)中的使用都是必不可少的。

【讨论】:

  • 总是尝试保留一个答案并在需要时更新它,而不是多个答案。
猜你喜欢
  • 2020-07-19
  • 1970-01-01
  • 2013-09-10
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
相关资源
最近更新 更多