【问题标题】:add objects to Binary Search tree using custom comparator?使用自定义比较器将对象添加到二叉搜索树?
【发布时间】:2013-10-18 05:04:02
【问题描述】:

我需要将对象添加到二叉搜索树并想编写自己的 compareTo 方法。我该怎么做。实际上我正在尝试实现自定义 TreeSet。我很困惑在哪里实现比较器或可比较并定义它。任何帮助表示赞赏

【问题讨论】:

    标签: java collections binary-search-tree comparable treeset


    【解决方案1】:

    您的对象必须实现接口 Comparable

    class MyClass implements Comparable { 
      int compareTo(){...}
    }
    final Set set = new TreeSet();
    set.add(new MyClass());
    ....
    set.add(new MyClass());
    

    或制作我们自己的比较器:

    class MyClass {}
    
    class MyComparator implements Comparator {
        int compareTo(Object o1, Object o2){
          ...
        }
    }
    final Set set = new TreeSet(new MyComparator());
    set.add(new MyClass());
    ....
    set.add(new MyClass());
    

    【讨论】:

      【解决方案2】:

      您要确保添加到TreeSet 的对象实现Comparable。这意味着确保存储的对象实现compareTo 中定义的Javadoc 方法。

      您可能不需要Comparator

      顺便说一句,我们最近创建了一个 tutorial 正是关于这个主题。希望它可以帮助你。

      【讨论】:

        【解决方案3】:

        请参考以下代码

        package com.example.treeset;
        
            import java.util.Comparator;
            import java.util.TreeSet;
        
            public class MyCompUser {
        
                public static void main(String a[]){
                    //By using name comparator (String comparison)
                    TreeSet<Empl> nameComp = new TreeSet<Empl>(new MyNameComp());
                    nameComp.add(new Empl("Ram",3000));
                    nameComp.add(new Empl("John",6000));
                    nameComp.add(new Empl("Crish",2000));
                    nameComp.add(new Empl("Tom",2400));
                    for(Empl e:nameComp){
                        System.out.println(e);
                    }
                    System.out.println("===========================");
                    //By using salary comparator (int comparison)
                    TreeSet<Empl> salComp = new TreeSet<Empl>(new MySalaryComp());
                    salComp.add(new Empl("Ram",3000));
                    salComp.add(new Empl("John",6000));
                    salComp.add(new Empl("Crish",2000));
                    salComp.add(new Empl("Tom",2400));
                    for(Empl e:salComp){
                        System.out.println(e);
                    }
                }
            }
        
            class MyNameComp implements Comparator<Empl>{
        
                @Override
                public int compare(Empl e1, Empl e2) {
                    return e1.getName().compareTo(e2.getName());
                }
            }   
        
            class MySalaryComp implements Comparator<Empl>{
        
                @Override
                public int compare(Empl e1, Empl e2) {
                    if(e1.getSalary() > e2.getSalary()){
                        return 1;
                    } else {
                        return -1;
                    }
                }
            }
        
            class Empl{
        
                private String name;
                private int salary;
        
                public Empl(String n, int s){
                    this.name = n;
                    this.salary = s;
                }
        
                public String getName() {
                    return name;
                }
                public void setName(String name) {
                    this.name = name;
                }
                public int getSalary() {
                    return salary;
                }
                public void setSalary(int salary) {
                    this.salary = salary;
                }
                public String toString(){
                    return "Name: "+this.name+"-- Salary: "+this.salary;
                }
            }
        

        【讨论】:

          【解决方案4】:

          对于TreeSet,您需要提供自己的Comparator 接口实现。

          可能是这样的吧?

          // Using an anonymous interface
          Set<Foobar> example = new TreeSet<Foobar>(new Comparator<Foobar>() {
            @Override
            public int compare(Foobar f1, Foobar f2) {
              // How will you compare f1 and f2??
              return 0;
            }
          
            @Override
            public boolean equals(Object obj) {
              // How will you determine if objects are equal?
              return false;
            }
          }
          );
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-12-06
            • 2011-11-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多