简体   繁体   English

使用比较器对Java TreeSet进行排序

[英]Sort a Java TreeSet using Comparator

i am trying to sort a TreeSet of objects ("Etudiant") using Comparator interface . 我正在尝试使用Comparator接口对TreeSet对象(“ Etudiant”)进行排序。 This the Comparator implementation: 这是Comparator实现:

import java.util.Comparator;


public class TriParNom implements Comparator<Etudiant>{
    public int compare(Etudiant o1, Etudiant o2) {
        return o1.getNom().compareTo(o2.getNom());
    }
}

here is the the TreeSet declaration and the call of the comparator in the main : 这是TreeSet声明和main中的比较器调用:

TreeSet<Etudiant> University= new TreeSet<Etudiant>(new TriParNom());

the error i get in the main class when i declare the TreeSet and call the comparator ,is : no suitable constructor found for TreeSet(TriParNom) . 当我声明TreeSet并调用比较器时,我在主类中遇到的错误是:没有找到适合TreeSet(TriParNom)的构造函数。

Any solutions please ? 有什么解决办法吗? thanks in advance . 提前致谢 。

I tried a very simple implementation based on the information you provided, and I give you my results: 我根据您提供的信息尝试了一个非常简单的实现,并给出了我的结果:

项目结构

The Etudiant class is a very simple pojo Etudiant类是一个非常简单的pojo

public class Etudiant {

private String nom;

public String getNom() {
    return nom;
}

public void setNom(String nom) {
    this.nom = nom;
}   }

The TriParNom class is the plain Comparator you described: TriParNom类是您描述的普通比较器:

import java.util.Comparator;

public class TriParNom implements Comparator<Etudiant> {

    @Override
    public int compare(Etudiant o1, Etudiant o2) {
        return o1.getNom().compareTo(o2.getNom());
    }

}

And here is a simple class with an entry point and a sample method to exercise the newly created treeset 这是一个简单的类,带有一个入口点和一个示例方法,用于练习新创建的树集

import java.util.TreeSet;

public class Main {

    public static void main(String[] args) {
        TreeSet<Etudiant> u = new TreeSet<>(new TriParNom());
        System.out.printf("size? %d%n", u.size());      
    }

}

Execution results follow: 执行结果如下:

EXEC

Apparently, there are no compilation errors either. 显然,也没有编译错误。

If your code matches to the snippet given below, then it should run fine without problems. 如果您的代码与下面给出的代码段匹配,那么它应该可以正常运行而不会出现问题。 The moment you remove the part implements Comparator<Etudiant> from class TriParNom , you will get the error indicating suitable constructor not found. class TriParNom删除零件implements Comparator<Etudiant>的那一刻,您将得到指示未找到合适的构造函数的错误。 Now, one another silly way it could happen if you haven't recompiled your classes after you implemented the comparator to your TriParNom - but that's too obvious. 现在,如果在实现TriParNom的比较器后没有重新编译类,则可能会发生另一种愚蠢的方式-但这太明显了。 Have your class that contins main method(that declares Treeset) imported java.util.TreeSet ? 将延续main方法(声明Treeset)的类导入java.util.TreeSet吗?

import java.util.Comparator;
import java.util.TreeSet;

public class TreesetCheck {
    public static void main(String[] args) {
        TreeSet<Etudiant> University= new TreeSet<Etudiant>(new TriParNom());
    }
}

class TriParNom implements Comparator<Etudiant>{
    public int compare(Etudiant o1, Etudiant o2) {
        return o1.getNom().compareTo(o2.getNom());
    }
}

class Etudiant {
    public String getNom() {
        // TODO Auto-generated method stub
        return "some";
    }
}

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

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