Java—使库类具有可比性

Java—使库类具有可比性,java,comparator,comparable,anonymous-class,Java,Comparator,Comparable,Anonymous Class,是否可以在不扩展库类的情况下使库类具有可比性 import org.json.JSONObject; LinkedList<JSONObject> list = getListFromFunction(); TreeSet<JSONObject> treeSet = new TreeSet<JSONObject>(list); import org.json.JSONObject; LinkedList=getListFromFunction(); 树集

是否可以在不扩展库类的情况下使库类具有可比性

import org.json.JSONObject;

LinkedList<JSONObject> list = getListFromFunction();

TreeSet<JSONObject> treeSet = new TreeSet<JSONObject>(list);
import org.json.JSONObject;
LinkedList=getListFromFunction();
树集树集=新树集(列表);

在此处创建
TreeSet
是不可能的,因为
JSONObject
是不可比较的。如何将自定义比较器“附加”到
JSONObject

(有一个独特的属性,比如说“\u some\u id”进行比较)

我们可以在这种情况下使用比较器并处理该场景。请参考下面的例子

主类

public class ComparatorTest{
     public static void main(String[] ar) {
        // System.out.println(new Sample().stringTimes("vivek", 5));
        JSONObject emp1 = new JSONObject();
        JSONObject emp2 = new JSONObject();
        try {
            emp1.put("department", "IT");
            emp1.put("name", "bvivek");
            emp1.put("id", 1);

            emp2.put("department", "IT");
            emp2.put("name", "avikash");
            emp2.put("id", 2);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<JSONObject> employess = new ArrayList<JSONObject>();
        employess.add(emp1);//add to list
        employess.add(emp2);//add to list
        System.out.println(employess);//unsorted, as is
        Collections.sort(employess, new JSONComparator("name"));
        System.out.println(employess);//sorted as per the field
        //using treeSet
        TreeSet<JSONObject> jsonInTree = new TreeSet<JSONObject>(new JSONComparator("id"));
        jsonInTree.addAll(employess);
        System.out.println(jsonInTree);//using the tree implementation
    }
}
公共类比较测试{
公共静态void main(字符串[]ar){
//System.out.println(新示例().stringTimes(“vivek”,5));
JSONObject emp1=新的JSONObject();
JSONObject emp2=新的JSONObject();
试一试{
emp1.put(“部门”、“IT”);
emp1.put(“名称”、“bvivek”);
emp1.put(“id”,1);
emp2.put(“部门”、“IT”);
emp2.put(“名称”、“阿维卡什”);
emp2.put(“id”,2);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
List employess=new ArrayList();
employess.add(emp1);//添加到列表
employess.add(emp2);//添加到列表
System.out.println(employess);//按原样未排序
Collections.sort(雇员、新JSONComparator(“名称”);
System.out.println(employess);//按字段排序
//使用树集
TreeSet jsonInTree=新的TreeSet(新的JSONComparator(“id”));
jsonInTree.addAll(雇员);
System.out.println(jsonInTree);//使用树实现
}
}
JSONComparator

class JSONComparator implements Comparator<JSONObject> {
    private String fieldToCompare;

    public JSONComparator(String fieldToCompare) {
        this.fieldToCompare = fieldToCompare;
    }

    @Override
    public int compare(JSONObject o1, JSONObject o2) {
        String id1 = "";
        String id2 = "";
        try {
            id1 = o1.getString(this.fieldToCompare);
            id2 = o2.getString(this.fieldToCompare);
        } catch (JSONException e) {

        }

        return id1.compareTo(id2);
    }
}
类JSONComparator实现Comparator{
私有字符串字段比较;
公共JSONComparator(字符串字段比较){
this.fieldToCompare=fieldToCompare;
}
@凌驾
公共整数比较(JSONObject o1,JSONObject o2){
字符串id1=“”;
字符串id2=“”;
试一试{
id1=o1.getString(this.fieldToCompare);
id2=o2.getString(this.fieldToCompare);
}捕获(JSONException e){
}
返回id1.compareTo(id2);
}
}

在这种情况下,我们可以使用Comparator来处理该场景。请参考下面的例子

主类

public class ComparatorTest{
     public static void main(String[] ar) {
        // System.out.println(new Sample().stringTimes("vivek", 5));
        JSONObject emp1 = new JSONObject();
        JSONObject emp2 = new JSONObject();
        try {
            emp1.put("department", "IT");
            emp1.put("name", "bvivek");
            emp1.put("id", 1);

            emp2.put("department", "IT");
            emp2.put("name", "avikash");
            emp2.put("id", 2);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<JSONObject> employess = new ArrayList<JSONObject>();
        employess.add(emp1);//add to list
        employess.add(emp2);//add to list
        System.out.println(employess);//unsorted, as is
        Collections.sort(employess, new JSONComparator("name"));
        System.out.println(employess);//sorted as per the field
        //using treeSet
        TreeSet<JSONObject> jsonInTree = new TreeSet<JSONObject>(new JSONComparator("id"));
        jsonInTree.addAll(employess);
        System.out.println(jsonInTree);//using the tree implementation
    }
}
公共类比较测试{
公共静态void main(字符串[]ar){
//System.out.println(新示例().stringTimes(“vivek”,5));
JSONObject emp1=新的JSONObject();
JSONObject emp2=新的JSONObject();
试一试{
emp1.put(“部门”、“IT”);
emp1.put(“名称”、“bvivek”);
emp1.put(“id”,1);
emp2.put(“部门”、“IT”);
emp2.put(“名称”、“阿维卡什”);
emp2.put(“id”,2);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
List employess=new ArrayList();
employess.add(emp1);//添加到列表
employess.add(emp2);//添加到列表
System.out.println(employess);//按原样未排序
Collections.sort(雇员、新JSONComparator(“名称”);
System.out.println(employess);//按字段排序
//使用树集
TreeSet jsonInTree=新的TreeSet(新的JSONComparator(“id”));
jsonInTree.addAll(雇员);
System.out.println(jsonInTree);//使用树实现
}
}
JSONComparator

class JSONComparator implements Comparator<JSONObject> {
    private String fieldToCompare;

    public JSONComparator(String fieldToCompare) {
        this.fieldToCompare = fieldToCompare;
    }

    @Override
    public int compare(JSONObject o1, JSONObject o2) {
        String id1 = "";
        String id2 = "";
        try {
            id1 = o1.getString(this.fieldToCompare);
            id2 = o2.getString(this.fieldToCompare);
        } catch (JSONException e) {

        }

        return id1.compareTo(id2);
    }
}
类JSONComparator实现Comparator{
私有字符串字段比较;
公共JSONComparator(字符串字段比较){
this.fieldToCompare=fieldToCompare;
}
@凌驾
公共整数比较(JSONObject o1,JSONObject o2){
字符串id1=“”;
字符串id2=“”;
试一试{
id1=o1.getString(this.fieldToCompare);
id2=o2.getString(this.fieldToCompare);
}捕获(JSONException e){
}
返回id1.compareTo(id2);
}
}

这样做的最简单方法适用于任何不可比较的类。您可以通过创建自己的比较方法来执行此操作,方法如下:

public static int compareJSONObjects(JSONObject obj1, JSONObject obj2){
    if(obj1.getField()>obj2.getField()){
        return 1;
    }else{
        return -1;
    }
}
现在,当调用list.sort()时,您可以创建自己的比较器,如下所示:

list.sort( (obj1, obj2) -> compareJSONObject(obj1, obj2) );
通过这样做,您可以减少所需的行数,因为通过使用和执行以下操作,整个过程可以缩短为一行:

list.sort( (obj1, obj2) -> obj1.getField()>obj2.getField() ? 1 : -1 );

这样做的最简单方法适用于任何不可比较的类。您可以通过创建自己的比较方法来执行此操作,方法如下:

public static int compareJSONObjects(JSONObject obj1, JSONObject obj2){
    if(obj1.getField()>obj2.getField()){
        return 1;
    }else{
        return -1;
    }
}
现在,当调用list.sort()时,您可以创建自己的比较器,如下所示:

list.sort( (obj1, obj2) -> compareJSONObject(obj1, obj2) );
通过这样做,您可以减少所需的行数,因为通过使用和执行以下操作,整个过程可以缩短为一行:

list.sort( (obj1, obj2) -> obj1.getField()>obj2.getField() ? 1 : -1 );

为什么不传入一个
比较器
?(真的,这是你唯一的选择。)为什么不传入一个
比较器呢?(真的,这是你唯一的选择。)我如何将排序结果插入到只允许可比较对象的树集?你可以使用比较器创建树集,然后将所有元素添加到树集。请参阅我的更新答案。谢谢。我与C++语法混淆了自定义比较器。我可以把说排序的结果插入到树集中,这样就可以比较对象,你可以通过使用比较器创建树集,然后将所有元素添加到树集。请参考我的更新答案。谢谢。我对C++比较器的自定义比较器感到困惑。