多场收集排序

时间:2013-07-11 04:07:17

标签: java class sorting arraylist

我有一个类Arraylist包含值 String word,String expandedword,double confidence,double support

我想基于信心对arraylist进行排序,然后基于支持。

我已经成功地根据信心对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那样制作另一种方法,根据支持值对其进行排序。 如何首先通过置信度对其进行排序,然后再使用支持值对其进行排序?

3 个答案:

答案 0 :(得分:3)

此用户比较器。由于可比较提供了基于单一类型排序的功能。 这里是您发现何时使用可比较和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(<List>, <comparator>)即可获得所需的列表。

只有在您想要在保密基础或支持基础上进行排序时才需要这样做 但是如果你需要那么首先按照confidance进行排序,如果confidance相等,那么检查支持依据。那么可比性就足够了

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;
}

答案 1 :(得分:1)

尝试使用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;
}

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

答案 2 :(得分:0)

我看到你的回复表示你要排序一次,然后再次排序,不同之处,所以我假设您想在排序时添加自定义Comparator。这是你正在寻找的吗?

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等其他集合中的使用非常重要。

相关问题